使用dplyr筛选≥0的等待天数时如何保留NA值?
解决方法
- 显式将NA值纳入筛选条件,利用
is.na()组合逻辑判断:liver <- liver %>% filter(referral_assessment_time >= 0 | is.na(referral_assessment_time)) - 另一种简洁写法,用
coalesce把NA转换为符合条件的默认值(比如0):liver <- liver %>% filter(coalesce(referral_assessment_time, 0) >= 0)
说明
R中逻辑运算遇到NA会返回NA,而filter默认会剔除判断结果为NA的行。你之前尝试的rm.na=FALSE无效,因为filter函数并不支持该参数,这个参数通常用于na.omit等其他函数。
内容的提问来源于stack exchange,提问作者s_greene
相关产品推荐
相关产品推荐

