Home > other >  Add columns with values based on another column using mutate?
Add columns with values based on another column using mutate?

Time:02-24

I want to add a column (type) to my metadata table that lists what type of sample it is based on information from the id column.

  id    owner  
  <chr> <chr>  
1 R1234 personA
2 R5678 personA
3 PAT12 personB
4 PAT34 personB
5 MOCK1 personB
6 MOCK2 personB

I used the code below to add the owner column which was simple enough because there are only two options (personA or personB).

tibble %>%
  mutate(owner = if_else(str_detect(tibble$id,"^R.*"),"personA","personB"))

I am struggling with the final column because I need there to be three different options (reactor, patient, or mock) like this:

# A tibble: 6 × 3
  id    owner   type   
  <chr> <chr>   <chr>  
1 R1234 personA reactor
2 R5678 personA reactor
3 PAT12 personB patient
4 PAT34 personB patient
5 MOCK1 personB mock   
6 MOCK2 personB mock 

I'm sure the answer is not complicated, I'm just struggling to wrap my head around it. Any help appreciated!

CodePudding user response:

You can use case_when (like pointed out in the comments). I used substr for the condition but you can use any string filter of your choice instead.

library(dplyr)

df %>% 
  mutate(type = case_when(
                  substr(id,1,1)=="R" ~ "reactor", 
                  substr(id,1,1)=="P" ~ "patient", 
                  substr(id,1,1)=="M" ~ "mock"))
     id   owner    type
1 R1234 personA reactor
2 R5678 personA reactor
3 PAT12 personB patient
4 PAT34 personB patient
5 MOCK1 personB    mock
6 MOCK2 personB    mock

CodePudding user response:

Create a named lookup character vector, then match on 1st letter:

lookup <- setNames(c("reactor", "patient", "mock"), c("R", "P", "M"))

df$type <- lookup[ substr(df$id, 1, 1) ]

df
#      id   owner    type
# 1 R1234 personA reactor
# 2 R5678 personA reactor
# 3 PAT12 personB patient
# 4 PAT34 personB patient
# 5 MOCK1 personB    mock
# 6 MOCK2 personB    mock

CodePudding user response:

dat %>% mutate(type=case_when(
  grepl("^R",id)~"reactor",
  grepl("^P",id)~"patient",
  grepl("^M",id)~"mock")
)  

Output:

  id    owner   type   
  <chr> <chr>   <chr>  
1 R1234 personA reactor
2 R5678 personA reactor
3 PAT12 personB patient
4 PAT34 personB patient
5 MOCK1 personB mock   
6 MOCK2 personB mock 
  • Related