来自数据帧中的行值的平均值,不包括R中的最小值和最大值 [英] mean from row values in a dataframe excluding min and max values in R

查看:99
本文介绍了来自数据帧中的行值的平均值,不包括R中的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下数据框,df(显示在此处的片段):

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

我想做的是在删除两个极值后,计算每行的平均值。
对于整行我使用plyr:

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,]) ) )

创建一些临时数据框架/矩阵应该也是可以的,最小值和最大值被替换为NAs,但作为一个初学者,我无法做到这一点。

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.

非常感谢您的帮助

编辑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.

编辑2
没有plyr的部分解决方案:

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.

推荐答案

您可以使用 trim 参数意味着

apply(x,1,mean,trim=1/NCOL(x))
#         E1         E2         E3         E4         E5 
# 17.0980000  0.8765000  0.4376667  2.9583333  1.3295000

这篇关于来自数据帧中的行值的平均值,不包括R中的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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