在R中使用min()返回NA而不是Inf

编程入门 行业动态 更新时间:2024-10-26 20:21:22
本文介绍了在R中使用min()返回NA而不是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

本文发布于:2023-07-10 08:28:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1089841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   min   NA   Inf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!