英文:
I would like to filter without losing NAs
问题
liver <- liver %>% filter(referral_assessment_time >= 0, !is.na(referral_assessment_time))
英文:
I have a dataset (liver) where rows are patients and one column is how many days the patient waited between referral and assessment (referral_assessment_time).
I would like to filter the number of days patients waited between referral and assessment to greater than or equal to 0 days, but I keep losing the NAs despite trying some versions of rm.na = FALSE. I'd be very grateful for your help with how to keep the NAs.
liver <- liver %>% filter(referral_assessment_time >=0)
I think the question is different from "How to filter data without losing NA rows using dplyr" as that question seemed to be about strings and mine is about numbers and I can't apply that answer to my question. I'd be very grateful if @akrun reply could please be added back on here, many thanks!
答案1
得分: 2
另一种选择是coalesce
它:
data.frame(a=c(1, NA, 5)) %>%
filter(coalesce(a, Inf) > 3)
# a
# 1 NA
# 2 5
英文:
An alternative would be to coalesce
it:
data.frame(a=c(1, NA, 5)) %>%
filter(coalesce(a, Inf) > 3)
# a
# 1 NA
# 2 5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论