Search code examples
rcase

Condition on multiple columns in R


I want to create a code that run on a data frame based on conditions form another data frame. The code should do this: if column 1 has A and type1, then give it a score. the condition data frame is something similar to this:

df1<-data.frame(Gene1=c("A","B", "C","D"),
                   type1=c(50,40,20,10),
                   type2=c(20,30,20,10),
                   type3=c(5,50,30,10))

and my data looks like this:

df2<-data.frame(Gene1=c("A","B", "C","D"),
                types=c("type1","type2","type3","type4"))

the output should look like this :

df.output<-data.frame(Gene1=c("A","B", "C","D"),
                types=c("type1","type2","type3","type4"),
                score=c(50,30,30,10))

So the conditions is based on 2 columns, I used case_when but it did not work, something like this:

df<- df1%>%
mutate(score=case_when(Gene1=="A"& types=="type1~ 50,
Gene1=="B"& types=="type2~ 30,
Gene1=="C"& types=="type3~ 30,
Gene1=="D"& types=="type4~ 10,
 TRUE ~ 0))

it is not working, also if I want case_when to ignore the order of the conditions so it matches all the conditions regardless there order what should i do? Any help is appreciated.


Solution

  • library(tidyverse)
    
    df_output <- df1 |>
      left_join(df2, by = "Gene1") |> 
      mutate(score = case_when(
          Gene1 == "A" & types == "type1" ~ 50,
          Gene1 == "B" & types == "type2" ~ 30,
          Gene1 == "C" & types == "type3" ~ 30,
          Gene1 == "D" & types == "type4" ~ 10,
          TRUE ~ 0)) |>
      select(Gene1, types, score)