Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中聚合NAs_R_Aggregate_Nan_Na - Fatal编程技术网

在R中聚合NAs

在R中聚合NAs,r,aggregate,nan,na,R,Aggregate,Nan,Na,我在计算聚合平均值时处理NAs时遇到问题。请参阅以下代码: tab=data.frame(a=c(1:3,1:3), b=c(1,2,NA,3,NA,NA)) tab a b 1 1 1 2 2 2 3 3 NA 4 1 3 5 2 NA 6 3 NA attach(tab) aggregate(b, by=list(a), data=tab, FUN=mean, na.rm=TRUE) Group.1 x 1 1 2 2 2 2 3

我在计算聚合平均值时处理NAs时遇到问题。请参阅以下代码:

tab=data.frame(a=c(1:3,1:3), b=c(1,2,NA,3,NA,NA))
tab
  a  b
1 1  1
2 2  2
3 3 NA
4 1  3
5 2 NA
6 3 NA

attach(tab)
aggregate(b, by=list(a), data=tab, FUN=mean, na.rm=TRUE)
  Group.1   x
1       1   2
2       2   2
3       3 NaN
如果向量有所有的NAs,我想要NA而不是NaN,也就是说,我想要输出为

  Group.1   x
1       1   2
2       2   2
3       3  NA
我尝试使用自定义函数:

adjmean=function(x) {if(all(is.na(x))) NA else mean(x,na.rm=TRUE)}
但是,我得到以下错误:

aggregate(b, by=list(a), data=tab, FUN=adjmean)

Error in FUN(X[[1L]], ...) : 
  unused argument (data = list(a = c(1, 2, 3, 1, 2, 3), b = c(1, 2, NA, 3, NA, NA)))
简而言之,如果列包含所有NAs,我希望将NA作为输出,而不是NaN。如果NAs很少,则应计算忽略NAs的平均值

任何帮助都将不胜感激


谢谢

这与您所拥有的非常接近,但将
平均值(x,na.rm=TRUE)
替换为一个自定义函数,该函数要么计算非na值的平均值,要么提供na本身:

R> with(tab, 
        aggregate(b, by=list(a), FUN=function(x) 
             if (any(is.finite(z<-na.omit(x)))) mean(z) else NA))
  Group.1  x
1       1  2
2       2  2
3       3 NA
R> 
R>带有(选项卡,
聚合(b,by=列表(a),FUN=函数(x)
如果(any)是有限的(z
这确实是一行,但我将其拆分以使其适合SO显示


您已经有了类似的想法,但是我对函数做了更多的修改,以便在所有情况下都返回合适的值。

您的函数没有任何问题。问题是您在默认方法中使用了一个不存在的参数作为聚合的参数:

adjmean = function(x) {if(all(is.na(x))) NA else mean(x,na.rm=TRUE)}
attach(tab)  ## Just because you did it. I don't recommend this.

## Your error
aggregate(b, by=list(a), data=tab, FUN=adjmean)
# Error in FUN(X[[i]], ...) : 
#   unused argument (data = list(a = c(1, 2, 3, 1, 2, 3), b = c(1, 2, NA, 3, NA, NA)))

## Dropping the "data" argument
aggregate(b, list(a), FUN = adjmean)
#   Group.1  x
# 1       1  2
# 2       2  2
# 3       3 NA

如果要使用
数据
参数,则应使用
公式
方法进行
聚合
。但是,此方法对
NA
的处理方式不同,因此需要一个附加参数
NA.action

例如:

detach(tab) ## I don't like having things attached
aggregate(b ~ a, data = tab, adjmean)
#   a b
# 1 1 2
# 2 2 2
aggregate(b ~ a, data = tab, adjmean, na.action = na.pass)
#   a  b
# 1 1  2
# 2 2  2
# 3 3 NA

您在此处使用
attach
?或将其与
data=tab
?@davidernburg结合使用的任何原因,都是将
聚合的公式方法和基本方法混为一谈。默认值(非公式)方法没有
data
参数,因此需要
attach
with
$
。多谢了,德克……我发现您的语法也适用于我的函数……即(tab,aggregate(b,by=list(a),FUN=adjmean))工作…想知道为什么使用聚合的其他语法在这里不起作用…不客气。请随意接受(单击“勾号”仅限您作为问题所有者查看)和/或“向上投票”(单击“向上三角形”)——这就是StackOverflow的工作原理。@DirkEddelbuettel,对不起,但有……感谢Ananda解释这一点