数据框中行值的平均值,不包括 R 中的最小值和最大值
我得到以下数据框,df(此处显示的片段):
H2475 H2481 H2669 H2843 H2872 H2873 H2881 H2909
E1 94.470 26.481 15.120 18.490 16.189 11.422 14.886 0.512
E2 1.016 0.363 0.509 1.190 1.855 0.958 0.771 0.815
E3 9.671 0.637 0.571 0.447 0.116 0.452 0.403 0.003
E4 3.448 2.826 2.183 2.607 4.288 2.526 2.820 3.523
E5 2.548 1.916 1.126 1.553 1.089 1.228 0.887 1.065
我想要做的是在删除两个极值后计算每行的平均值。 对于整行,我使用 plyr:
library(plyr)
df.my_means <- adply(df, 1, transform, my_means = mean(as.matrix(df[i,]) ) )
创建一些临时数据框/矩阵,并将最小值和最大值替换为 NA 应该也可以,但作为初学者,我无法做到这一点。
非常感谢您的帮助
编辑1
我显然不知道mean有一个修剪选项。我想要一个解决方案,我可以插入任何其他函数,而不是 mean 。即:
library(plyr)
library(e1071)
df.my_means <- adply(df, 1, transform, my_skew = skewness(as.matrix(df[i,]), , 3 ) )
如果这违反了问题发布规则,我深表歉意,但是针对平均值、中位数等单独提出问题是违反直觉的。
编辑2 没有 plyr 的部分解决方案:
df.my_means <- apply(df ,1, function(x){y=x[order(x)]; (y[2:(length(y)-1)])})
这会破坏列值之间的连接。
I got following data frame,df, (fragment displayed here):
H2475 H2481 H2669 H2843 H2872 H2873 H2881 H2909
E1 94.470 26.481 15.120 18.490 16.189 11.422 14.886 0.512
E2 1.016 0.363 0.509 1.190 1.855 0.958 0.771 0.815
E3 9.671 0.637 0.571 0.447 0.116 0.452 0.403 0.003
E4 3.448 2.826 2.183 2.607 4.288 2.526 2.820 3.523
E5 2.548 1.916 1.126 1.553 1.089 1.228 0.887 1.065
what I want to do is to compute mean values of each row after removing two extreme values.
For whole rows I used plyr:
library(plyr)
df.my_means <- adply(df, 1, transform, my_means = mean(as.matrix(df[i,]) ) )
It should be also OK to create some temporary data frame/matrix with min and max values replaced by NAs, but as a beginner I am not able to do it.
Thanks a lot for your help
EDIT 1
I was obviously unaware that mean has a trim option. I would like to have a solution where instead of mean I can plug in any other function. I.e.:
library(plyr)
library(e1071)
df.my_means <- adply(df, 1, transform, my_skew = skewness(as.matrix(df[i,]), , 3 ) )
I apologize if this breaks the question posting rules, but then having separate questions for mean, median etc. is counter-intuitive.
EDIT 2
Partial solution without plyr:
df.my_means <- apply(df ,1, function(x){y=x[order(x)]; (y[2:(length(y)-1)])})
This break the connection between column values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
trim
参数用于mean
:You can use the
trim
argument tomean
: