R -is.na在ifelse()中的www.example.com()

mccptt67  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(131)

我尝试使用mutate()在我的数据集titanic中创建一个新变量child_or_adult。新变量将指定每个人的年龄组。然而,变量age包含NA的值,我不想忽略这些值。相反,我想把它们分类为adult,我怎么能在ifeslse()语句中使用is.na()呢?

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

whlutmcx1#

尝试这样做:

titanic %>% mutate(child_or_adult = ifelse(age <= 9 | !is.na(age),"child","adult"))

相关问题