Home > Software engineering >  Case_when with NA dplyr
Case_when with NA dplyr

Time:11-09

I want to add a new column names (title_holder) to my dataset (image), based on the column "title". All NAs in the column title should have the value "no" in the new column "title_holder", else the value should be "yes".

Thanks.enter image description here

Among others I tried this code among others:

age_average <- age %>% mutate(title_holder = case_when (!is.na(title), "no", TRUE ~ "yes")) %>%view()

Can someone help me figuring out the correct code?

CodePudding user response:

We need the ~

library(dplyr)
age_average <- age %>% 
   mutate(title_holder = case_when(!is.na(title)~ "no", TRUE ~ "yes"))

CodePudding user response:

The solution by @akrun. Or in this case we could also use ifelse:

library(dplyr)
age_average <- df %>% mutate(title_holder = ifelse (!is.na(title), "no","yes")) %>%view()
    id  lastname firstname              title gender year_of_birth year_of_death    activity age title_holder
1   54   Heusser      Hans Prof. Dr. med. vet      m          1884          1978    Tierarzt  94           no
2   59   Kleiner Gottfried               <NA>      m          1897          1958    Kaufmann  61          yes
3 2512 Pfleghard      Otto               <NA>      m          1869          1958   Architekt  89          yes
4 2519    Wetter     Ernst     Dr. oec. publ.      m          1877          1963 Nationalrat  86           no

example data:

df <- structure(list(id = c(54L, 59L, 2512L, 2519L), lastname = c("Heusser", 
"Kleiner", "Pfleghard", "Wetter"), firstname = c("Hans", "Gottfried", 
"Otto", "Ernst"), title = c("Prof. Dr. med. vet", NA, NA, "Dr. oec. publ."
), gender = c("m", "m", "m", "m"), year_of_birth = c(1884L, 1897L, 
1869L, 1877L), year_of_death = c(1978L, 1958L, 1958L, 1963L), 
    activity = c("Tierarzt", "Kaufmann", "Architekt", "Nationalrat"
    ), age = c(94L, 61L, 89L, 86L)), class = "data.frame", row.names = c(NA, 
-4L))
  •  Tags:  
  • r
  • Related