extract two columns from dataframe and make into a vector

362 Views Asked by At

I have a table with 4 columns, I want to extract column 1 and 3 to make a character vector. My aim is to define colour for a phylogenetic plot

Table
name    id  colour  date    
t1  a   red 10/01
t2  b   red 09/02       
t3  c   black   04/04
t4  d   black   05/05

Desired vector
cols <- c("t1"="red", "t2"="red", "t3"="black", "t4"="black")

Ive tried the suggestion from here but Its not giving me what I want. Converting two columns of a data frame to a named vector

When I execute my command to plot the phylogeny the colour matrix provided in the vector does not comply. The colours do not match what I have in the table.

For more info see my previous thread I want to colour the lines in a tanglegram using cophyloplot in ape. Argument "col=" states a character vector indicating the color to be used for the links; recycled as necessary

1

There are 1 best solutions below

0
On

You can use the setNames function, for instance:

setNames(iris$Petal.Length, iris$Species)