Jon
Jon

Reputation: 601

R: fill rows in column by matching rows in another column

This has probably been asked many times, but I only find more complicated cases than mine, and I really don't know where to start. I need to add a new column (Condition) to my data frame and fill the rows according to the values in the column cellNr

My data frame is molten.pC:

  cellNr     value
1    G63  0.000000
2    G64  8.848623
3    G65  0.000000
4    G66 10.788718
5    B15  5.285402
6    B16  0.000000
7    B17  0.000000
8    C10  0.000000
9    C11  0.000000

I want to add a column Condition and fill it like this:

  cellNr     value     Condition
1    G63  0.000000  Growth
2    G64  8.848623  Growth
3    G65  0.000000  Growth
4    G66 10.788718  Growth
5    B15  5.285402  Burst
6    B16  0.000000  Burst
7    B17  0.000000  Burst
8    C10  0.000000  Cellularized
9    C11  0.000000  Cellularized

Upvotes: 1

Views: 659

Answers (1)

akrun
akrun

Reputation: 886928

We can do this in base R by extracting the first character (substr), convert to factor with labels and levels specified.

molten.pC$Condition <- as.character(factor(substr(molten.pC$cellNr, 1, 1), 
      levels = c("G", "B", "C"), labels = c("Growth", "Burst", "Cellularized")))
molten.pC$Condition
#[1] "Growth"       "Growth"       "Growth"       "Growth"       "Burst" 
#[6]  "Burst"        "Burst"        "Cellularized" "Cellularized"

Or we can use case_when from dplyr

library(dplyr) #devel version (soon to be released `0.6.0`)
molten.pC  %>% 
      mutate(Sub = substr(cellNr, 1, 1),
             Condition = case_when(Sub=="G" ~"Growth",
                                   Sub == "B" ~"Burst", 
                           TRUE ~"Cellularized")) %>%
      select(-Sub)
#  cellNr     value    Condition
#1    G63  0.000000       Growth
#2    G64  8.848623       Growth
#3    G65  0.000000       Growth
#4    G66 10.788718       Growth
#5    B15  5.285402        Burst
#6    B16  0.000000        Burst
#7    B17  0.000000        Burst
#8    C10  0.000000 Cellularized
#9    C11  0.000000 Cellularized

Upvotes: 2

Related Questions