Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mean from row values in a dataframe excluding min and max values in R

Tags:

dataframe

r

plyr

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.

like image 797
darked89 Avatar asked Jan 25 '26 07:01

darked89


1 Answers

You can use the trim argument to mean:

apply(x,1,mean,trim=1/NCOL(x))
#         E1         E2         E3         E4         E5 
# 17.0980000  0.8765000  0.4376667  2.9583333  1.3295000
like image 89
Joshua Ulrich Avatar answered Jan 27 '26 21:01

Joshua Ulrich