I have vectors Xval and index as follows
Xval <- c(-10, 5, 25, 10, 5, 21, 48, 4, 57, 14, 10, 15)
index <- c(1, 2, 2, 1, 3, 1, 3, 3, 1, 2, 1, 1)
I would like to replace index value conditioning on Xval_mean
Xval_mean <- tapply(Xval, index, mean)
# 1 2 3
# 17.16667 14.66667 19.00000
Replace the index value by 1 for which Xval_mean is maximum, by 2 for which Xval_mean is minimum, and by 3 for the middle value of Xval_mean.
I can do it for max and min separately as
index[index == which.max(Xval_mean)] <- 1
index[index == which.min(Xval_mean)] <- 2
and for middle value of Xval_mean, index should be 3. I need to do them together.
Any help is appreciated