我想在不丢失缺失值的情况下进行筛选。

huangapple go评论72阅读模式
英文:

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 &lt;- liver %&gt;% filter(referral_assessment_time &gt;=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)) %&gt;%
  filter(coalesce(a, Inf) &gt; 3)
#    a
# 1 NA
# 2  5

huangapple
  • 本文由 发表于 2023年2月10日 03:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定