Home > OS >  ifelse, is.na and replace
ifelse, is.na and replace

Time:11-29

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

CodePudding user response:

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

CodePudding user response:

You were very close. We can use a 2nd ifelse statement to do this.

df <- data.frame(q3 = c("Very easy", "Easy", NA, NA, NA),
                 times_30 = c("18/11/2021", "01/01/2021", "Not due", "No answer", "01/01/2021"))

df$q3 <- ifelse(is.na(df$q3), 
                 ifelse(!df$times_30 %in%  c("No answer", "Not due"),
                        "No answer", df$times),
                 df$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/01/2021

Or alternatively, matching a digit in times_30, and using transform to not have to type df$ so many times:

df <- df |>
  transform(q3 = ifelse(is.na(q3),
                        ifelse(grepl("\\d", times_30), "No answer", times_30),
                        q3))
  • Related