Home > Software engineering >  Replace according to column name in r
Replace according to column name in r

Time:08-31

I would like to replace according to the column name, according to another dataframe

I have this dataframe:

name<-c("luis", "John", "Leo")
"2022-08-01"<-c(NA,"yes","yes")
"2022-08-02"<-c("yes",NA,"yes")
"2022-08-03"<-c(NA,"yes",NA)
df<-data_frame(name,`2022-08-01`,`2022-08-02`,`2022-08-03`)

what does it look like:

  name  `2022-08-01` `2022-08-02` `2022-08-03`
  <chr> <chr>        <chr>        <chr>       
1 luis  NA           yes          NA          
2 John  yes          NA           yes         
3 Leo   yes          yes          NA 

and this other

date<-c("2022-08-01", "2022-08-02", "2022-08-03")
value<-c("a,b","a,c","d")
df2<-data_frame(date, value)

what does it look like:

  date       value
  <chr>      <chr>
1 2022-08-01 a,b  
2 2022-08-02 a,c  
3 2022-08-03 d

I want that according to the second dataframe it is replaced in the first dataframe, according to the date and only the rows that say yes. So that I get a result like the following:

    name    01/08/22    02/08/22    03/08/22
1   luis    NA             a,c        NA
2   John    a,b            NA         d
3   Leo     a,b            a,c        NA

CodePudding user response:

A dplyr way:

library(dplyr)

df <- 
  df |> 
  mutate(across(-name,
                ~ if_else(is.na(.), NA_character_, df2$value[date == cur_column()])))

Or base:


df[,-1] <- lapply(seq_along(df[,-1]),
                  \(x) ifelse(is.na(df[,-1][[x]]), NA_character_, df2$value[date == names(df[,-1])[x]]))

Output:

# A tibble: 3 × 4
  name  `2022-08-01` `2022-08-02` `2022-08-03`
  <chr> <chr>        <chr>        <chr>       
1 luis  NA           a,c          NA          
2 John  a,b          NA           d           
3 Leo   a,b          a,c          NA          
  •  Tags:  
  • r
  • Related