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.

3
  • 2
    You can also create a new dataframe with columns as panel and growth and then use match Commented Jul 25, 2018 at 1: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)? - for some more detail. Commented Jul 25, 2018 at 1:20
  • 1
    BTW colors in your case is a vector, not a list. Commented Jul 25, 2018 at 2:23

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!That covers me.
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.

Comments

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'])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.