Say I have this small data set in R.
Data = c(8,2,9,7,8,8,9,8,4,9,9,7,2,5,2,2,1,9,9,7)
So I am looking to get the average of the 2'nd 3'rd and 4'th smallest and largest values from the last 10 where all values the 10 and ranks should be variable.
In Excel the formula for the mins would be =AVERAGE(SMALL(A11:A20,ROW(INDIRECT("2:4"))))
Entered with Ctrl+Shift+Enter.
And the expected result is
val min max
8 NA NA
2 NA NA
9 NA NA
7 NA NA
8 NA NA
8 NA NA
9 NA NA
8 NA NA
4 NA NA
9 6.3333 8.6667
9 6.3333 9
7 7.3333 9
2 6 8.6667
5 5.3333 8.6667
2 3.6667 8.6667
2 2.6667 8.6666
1 2 8
9 2 8.3333
9 2 9
7 2 8.3333
In r this seems fairly simple, I get the 10 or x values sort the values and then average or do whatever operation on the appropriate 3 values, which is discussed here
The problem is that I can only do this in a loop which is slow. Ho can I get such complicated operations in a vectored function?