Reputation: 33
I have a big data frame where I am trying to figure out with NA values
q3 | times_30 |
---|---|
Very easy | 18/11/2021 |
Easy | 01/01/2021 |
NA | Not due |
NA | No answer |
NA | 01/02/2021 |
I am using
df$q3<-ifelse (is.na(df$q3), df$times_30,df$q3)
Where gives me
q3 | times_30 |
---|---|
Very easy | 18/11/2021 |
Easy | 01/01/2021 |
Not due | Not due |
No answer | No answer |
01/02/2021 | 01/02/2021 |
but I would like instead of replaced with the date at the NA of q3 to give me a "No answer", as the following
q3 | times_30 |
---|---|
Very easy | 18/11/2021 |
Easy | 01/01/2021 |
Not due | Not due |
No answer | No answer |
No answer | 01/02/2021 |
Upvotes: 0
Views: 766
Reputation: 15143
You may try using dplyr::case_when
library(dplyr)
df %>%
mutate(q3 = case_when(
is.na(q3) & times_30 == "Not due" ~ times_30,
is.na(q3) ~ "No answer",
TRUE ~ q3
))
q3 times_30
1 Very easy 18/11/2021
2 Easy 01/01/2021
3 Not due Not due
4 No answer No answer
5 No answer 01/02/2021
Upvotes: 0