简体   繁体   English

如何在聚合R中应用自定义函数

[英]How to apply custom function in aggregate R

everyone! 大家! I have tried to find similar question, but i didn't. 我试图找到类似的问题,但我没有。 Let's assume that we have a simple data frame like this 假设我们有一个像这样的简单数据框

enter image description here 在此处输入图片说明

I need to aggregate by group: aggregate(value~group, data=data, mean,na.rm=TRUE) But, when i use na.rm=TRUE , group 1 disappears. 我需要按组进行聚合: aggregate(value~group, data=data, mean,na.rm=TRUE)但是,当我使用na.rm=TRUE ,组1消失了。 It is absolutely necessary to use na.rm=TRUE , because in opposite case i will get incorrect averages. 绝对有必要使用na.rm=TRUE ,因为在相反情况下,我将得到不正确的平均值。 I also have tried to use custom function: 我也尝试使用自定义功能:

customMean<-function(x){
  if (all(is.na(x))){
      return (NA_integer_)
  } else {  
    return(mean(x,na.rm=TRUE))
  }
}

but, the result is same. 但是结果是一样的。 Does anybody know how to fix this issue? 有人知道如何解决此问题吗? I expect that final result will be like this: enter image description here . 我希望最终结果将是这样: 在此处输入图片描述 Thanks 谢谢

By default, the formula method of aggregate has na.action=na.omit . 默认情况下, aggregate的公式方法具有na.action=na.omit So, it removes the entire row if there is an NA value. 因此,如果有NA值,它将删除整行。 We can change it to na.action=NULL and it should work. 我们可以将其更改为na.action=NULL ,它应该可以工作。

aggregate(value~group, data=data, mean,na.rm=TRUE, na.action=NULL)
#    group value
#1     1   NaN
#2     2   2.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM