0

I have a small data frame:

gene_symbol<-c("DADA","SDAASD","SADDSD","SDADD","ASDAD","XCVXCVX","EQWESDA","DASDADS")
panel<-c("growth","growth","growth","growth","big","big","big","small")
Gene_states22<-data.frame(gene_symbol,panel)

and a vector with colors:

 colors<-c("red","green","yellow").

I would like to create a dataframe like this:

gene_symbol  panel  color
1        DADA growth    red
2      SDAASD growth    red
3      SADDSD growth    red
4       SDADD growth    red
5       ASDAD    big  green
6     XCVXCVX    big  green
7     EQWESDA    big  green
8     DASDADS  small yellow

In a few words to add a new column where "growth" matches to "red", "big" to "green" and "small" to "yellow". The issue is that the panel names wont be the same every time for example they may be "bob","sam","bill", and there may be up to 8 different names (and colors). Also the rows of the data frame will vary.

firmo23
  • 7,490
  • 2
  • 38
  • 114
  • 2
    You can also create a new dataframe with columns as `panel` and `growth` and then use `match` – Ronak Shah Jul 25 '18 at 01:18
  • 3
    Agree with @RonakShah and the answers below. I'd have a read up on join operations in R, whether using the included `merge` functions or an additional package - see this classic old question: [How to join (merge) data frames (inner, outer, left, right)?](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) - for some more detail. – thelatemail Jul 25 '18 at 01:20
  • 1
    BTW `colors` in your case is a vector, not a list. – ytu Jul 25 '18 at 02:23

3 Answers3

4

Give your vectors a name and then it can be simple matter of extracting colors based on their names.

names(colors) = c("growth", "big", "small")
Gene_states22$colors = colors[as.character(Gene_states22$panel)]
Gene_states22
#  gene_symbol  panel colors
#1        DADA growth    red
#2      SDAASD growth    red
#3      SADDSD growth    red
#4       SDADD growth    red
#5       ASDAD    big  green
#6     XCVXCVX    big  green
#7     EQWESDA    big  green
#8     DASDADS  small yellow
d.b
  • 32,245
  • 6
  • 36
  • 77
1

One approach: set up a second dataframe with two columns (panel and color) and then merge it to the first dataframe. This can be done without manually typing panel for the second dataframe. For example:

df1 <- data.frame(
    gene  = c("DADA","SDAASD","SADDSD","SDADD","ASDAD","XCVXCVX","EQWESDA","DASDADS"),
    panel = c("growth","growth","growth","growth","big","big","big","small")
)

 colors<-c("red","green","yellow")

 df2 <- cbind(unique(df$panel), colors)

 result <- merge(df1, df2, by="panel")

Be sure (or write more code to check) that you have the right number of colors for the unique number of panel values.

DanY
  • 5,920
  • 1
  • 13
  • 33
1

Just create a named vector mapping panel to color.

panel2color <- c(growth='red', big='green', small='yellow')
Gene_states22[, 'color'] <- panel2color[as.character(Gene_stats22[, 'panel'])]