R max 函数忽略不适用 [英] R max function ignore NA

查看:55
本文介绍了R max 函数忽略不适用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下工作代码.当我在不同的数据集上复制相同的东西时,我会收到错误:(

I have below working code. When i replicate same things on a different data set i get errors :(

#max by values
df <- data.frame(age=c(5,NA,9), marks=c(1,2,7), story=c(2,9,NA))
df

df$colMax <- apply(df[,1:3], 1, function(x) max(x[x != 9],na.rm=TRUE))
df

我尝试在更大的数据上做同样的事情,但收到警告,为什么?

I tried to do the same on a bigger data and I am getting warnings, why?

maindata$max_pc_age <- apply(maindata[,c(paste("Q2",1:18,sep="_"))], 1, function(x) max(x[x != 9],na.rm=TRUE))


50: In max(x[x != 9], na.rm = TRUE) :
  no non-missing arguments to max; returning -Inf

为了更好地理解问题,我进行了如下更改,但仍然收到警告

in order to understand the problem better I made changes as below, but still getting warnings

maindata$max_pc_age <- apply(maindata[,c(paste("Q2",1:18,sep="_"))], 1, function(x) max(x,na.rm=TRUE))
1: In max(x, na.rm = TRUE) : no non-missing arguments to max; returning -Inf

推荐答案

问题似乎已经在评论中指出了.由于某些向量仅包含 NA ,因此报告了 -Inf,这是我从您不喜欢的评论中获取的.在这个答案中,我想指出一种可能的方法来解决这个问题,即内置控制语句(而不是在事实之后覆盖 -Inf,这同样有效).例如,

It seems that the problem has been pointed out in the comments already. Since some vectors contain only NAs, -Inf is reported, which I take from the comments you don't like. In this answer I would like to point out one possible way to tackle the issue, namely to built in a control statement (instead of overwritting -Inf after the fact, which is equally valid). For instance,

 my.max <- function(x) ifelse( !all(is.na(x)), max(x, na.rm=T), NA)

做这个把戏.如果x中的每个(all)元素都是NA,则返回NA,并且max 否则.如果您想要返回任何其他值,只需将 NA 交换为该值.您还可以轻松地将其构建到您的 apply 函数中.例如.

does this trick. If every (all) element in x is NA, then NA is returned, and the max otherwise. If you want any other value returned, just exchange NA for that value. You can also built this easily into your apply-function. E.g.

 maindata$max_pc_age <- apply(maindata[,c(paste("Q2",1:18,sep="_"))], 1, my.max)

我有时仍然对 R 的 NA 和空集处理感到困惑.像 test <- NA; 这样的语句test==NA 将给出 NA 作为结果(而不是 TRUE,由 is.na(test) 返回),有时可以通过说由于缺失值来合理化,你怎么知道这两个缺失值是相同的?然而,在这种情况下, max 返回 -Inf 因为它被赋予了一个空集,我认为这并不明显.我的经验是,如果出现奇怪和意外的结果,通常会涉及 NA 或空集.

I am still sometimes confused by R's NA and empty set treatment. Statements like test <- NA; test==NA will give NA as a result (instead of TRUE, as returned by is.na(test)), which is sometimes rationalized by saying that since the value is missing, how could you know that these two missing values are identical? In this case, however, max returns -Inf since it is given an empty set, which I think is not at all obvious. My experience is though that if strange and unexpected results pop up, NAs or empty sets are often involved.

这篇关于R max 函数忽略不适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆