如何修正dplyr中case_when处理NA生成title_holder新列的代码?
问题修正与正确代码
你的代码存在两个核心问题:
- 数据集名称不匹配:需求是基于
image数据集处理,但代码里误用了age case_when语法与逻辑错误:条件和对应结果需用~分隔而非逗号,且你写的判断逻辑和需求完全相反(原代码是非NA时返回"no",但需求是NA时返回"no")
正确代码(使用case_when)
image_processed <- image %>% mutate(title_holder = case_when( is.na(title) ~ "no", TRUE ~ "yes" )) %>% view()
更简洁的替代写法(使用ifelse)
因为判断逻辑简单,用ifelse会更紧凑:
image_processed <- image %>% mutate(title_holder = ifelse(is.na(title), "no", "yes")) %>% view()
内容的提问来源于stack exchange,提问作者rosybrown-manderine
相关产品推荐
相关产品推荐

