[Solved] calculating mean for part of the data in R


> apply(x, 2, function(z) mean(z[order(z)][1:2]) )
[1] 1.5 6.5

Look at ?apply for row-wise or column-wise operations on matrices or all-numeric or all-character subsets of dataframes.

This is actually the lowest 40% of values, since 2/5 = 0.4. If you wanted this to be “sensitive” to varying lengths (and still asking for 40% rather than 20%) you would do something like:

> apply(x, 2, function(z) mean(z[order(z)][1:(length(z)*.4)]) )
[1] 1.5 6.5

solved calculating mean for part of the data in R