[英]Keep names from quantile() function when used in a data.table
[英]Using quantile function in data.table
我尝试计算data.table中某些值的平均值。 均值的计算应没有异常值,这意味着我必须首先过滤数据。 为了在我的数据中定义离群值,我使用“默认箱形图”方法。
这种方法非常有效:
test <- template[Month==1] # create small subset with data from January
qnt <- quantile(test$x,c(.25,.75)
H <- 1.5 * IQR(test$x)
test[x < (qnt[2]+H) & x > (qnt[1]-H),mean(add)]
[1] -6113.136
我也可以在数据表中进行计算:
test <- template[Month==1] # create small subset with data from January
test[x < (quantile(x,0.75)+1.5*IQR(x)) & x > (quantile(x,0.25)-1.5*IQR(x)),mean(x)]
[1] -6113.136
由于我不仅需要一月份的平均值,而且需要所有月份的平均值,所以我尝试这样做:
template[x < (quantile(x,0.75)+1.5*IQR(x)) & x > (quantile(x,0.25)-1.5*IQR(x)),mean(x),by=Month]
1: 1 -5601.3050 <<< not the same value than before
2: 2 1187.3186
...
最后一种方法的一月与之前的一月不同。 据我所知,quantile()方法就是问题所在。 我猜它不包括data.table的'by = Month'分组。
我的一般方法有什么问题吗? 有没有一种方法可以进行这些计算而无需先将“ template” data.table拆分成12个较小的表?
非常感谢您的帮助!
更新:
template[,.SD[x < (quantile(x,0.75)+1.5*IQR(x)) & x > (quantile(x,0.25)-1.5*IQR(x)),mean(x)],by=Month]
1: 1 -6113.1362
2: 2 1529.4808
感谢@Frank和@Roland的超快速支持!
template[,.SD[x < (quantile(x,0.75)+1.5*IQR(x)) & x > (quantile(x,0.25)-1.5*IQR(x)),mean(x)],by=Month]
弗兰克和罗兰说出来我需要改变我的表情,因为i
在精氨酸DT[i,j,by]
不通过分组使用。 使用.SD[]
是解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.