2
votes

I have a data frame with ~32k rows and 36 columns. I am trying to calculate the mean of each row and creating a separate column containing the mean for each row. I originally calculated it for columns 19:33 via data$mean <- data[,19:33] however now I want to exclude the highest and lowest values in each row to calculate the mean. Each row will have different high and low values in different locations. How can I effectively calculate the mean for each row while excluding those two values in each row?

1
apply(X = data[,19:33], MARGIN = 1, function(x) mean(replace(x, c(which.min(x), which.max(x)), NA), na.rm = TRUE))d.b
awesome! worked perfectly. Thanks!Rick
My answer worked ?BENY
I used db's and it worked fine, I did not use yours wenRick

1 Answers

2
votes

Try using apply, %in%,range

apply(df[,19:33], 1, function(x) mean(x[!x %in% range(x)]))