Reputation: 31
Each row in my dataset has 8 cases in it and I need to find the average of 7 of them, excluding the lowest value.
So, for instance, my first row goes as it follows:
15 13 8 22 20 12 8 8
So, the lowest value will be 8, then the average of the 7 remaining cases will be 14. Obviously, I wouldn't want to do this by hand so is there a function that will allow me to do it directly in R?
I used the apply(data,1,min)
function to find the lowest value for each row. Is there a way to get R to give me the average of each row whilst excluding the respective minimum values from this average?
Thanks!
Upvotes: 0
Views: 96
Reputation: 887048
We can find the index of lowest value with max.col
, cbind
with the row index, assign those elements to NA and get the rowMeans
m1[cbind(1:nrow(m1), max.col(-m1, "first"))] <- NA
rowMeans(m1, na.rm = TRUE)
#[1] 14.00000 14.28571
m1 <- rbind(c(15, 13, 8, 22, 20, 12, 8, 8), c(15, 13, 9, 23, 20, 12, 4, 8))
Upvotes: 2