在R中使用min()返回NA而不是Inf [英] With min() in R return NA instead of Inf
问题描述
请考虑以下内容:
我最近发现了很棒的 plyr
和 dplyr
软件包,并使用它们来分析在数据框中可用于我的患者数据。这样的数据帧可能看起来像这样:
I recently 'discovered' the awesome plyr
and dplyr
packages and use those for analysing patient data that is available to me in a data frame. Such a data frame could look like this:
df <- data.frame(id = c(1, 1, 1, 2, 2), # patient ID
diag = c(rep("dia1", 3), rep("dia2", 2)), # diagnosis
age = c(7.8, NA, 7.9, NA, NA)) # patient age
我想总结一下所有患者的最低年龄患者的中位数和均值。我做了以下事情:
I would like to summarise the minimum patient age of all patients with a median and mean. I did the following:
min.age <- df %>%
group_by(id) %>%
summarise(min.age = min(age, na.rm = T))
由于数据框中存在个NA
,我收到警告:
Since there are NAs
in the data frame I receive the warning:
`Warning message: In min(age, na.rm = T) :
no non-missing arguments to min; returning Inf`
使用 Inf
我无法致电摘要(df $ min.age)
。
使用 pmin( )
而不是 min
返回了错误消息:
Using pmin()
instead of min
returned the error message:
Error in summarise_impl(.data, dots) :
Column 'in.age' must be length 1 (a summary value), not 3
我该怎么做才能避免出现任何 Inf
而不是获得 NA
,以便我可以进一步进行以下操作:
summary(df $ min.age)
?
What can I do to avoid any Inf
and instead get NA
so that I can further proceed with:
summary(df$min.age)
?
非常感谢!
推荐答案
您可以使用 is.infinite()
检测无限,然后 ifelse
有条件地将其设置为 NA
。
You could use is.infinite()
to detect the infinities and ifelse
to conditionally set them to NA
.
#using your df and the dplyr package
min.age <-
df %>%
group_by(id) %>%
summarise(min.age = min(age, na.rm = T)) %>%
mutate(min.age = ifelse(is.infinite(min.age), NA, min.age))
这篇关于在R中使用min()返回NA而不是Inf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!