lundi 5 mars 2018

R - is.na() within ifelse()

I am trying to create a new variable 'child_or_adult' in my data set 'titanic', by using mutate(). The new variable would specify the age group of each person. However, the variable 'age' contains NAs values, which I don't want to ignore. Instead, I want to classify them as 'adult'. How could I do this by using is.na() within the ifeslse() statement ?

titanic %>% 
  mutate(child_or_adult = ifelse(test = age <= 9, 
                                 yes = "child", 
                                 no = "adult", 
                                 is.na(age) <- "adult"
                                 ))

Aucun commentaire:

Enregistrer un commentaire