You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修正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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 22:15:29