Let's say that I have a df contains ID, gender, and several numerical variables, and MAX1, MAX2, and MAX3, where
MAX1 = the variable name of the first max values of x1,x2,x3,x4,x5
MAX2 = the variable name of the second max values of x1,x2,x3,x4,x5
MAX3 = the variable name of the third max values of x1,x2,x3,x4,x5
### Generate data
set.seed(123)
ID <- c(1,2,3,4,5,6,7,8,9,10)
gender <- c("m", "m", "m", "f", "f", "m", "m", "f", "f", "m")
x1 <- rnorm(10, 0, 1)
x2 <- rnorm(10, 0, 1)
x3 <- rnorm(10, 0, 1)
x4 <- rnorm(10, 0, 1)
x5 <- rnorm(10, 0, 1)
df <- data.frame(ID, gender, x1, x2, x3, x4, x5)
maxes <- t(sapply(1:nrow(df), function(i) {
names(sort(df[i,3:7], decreasing=T)[1:3])
}))
colnames(maxes) <- c("MAX1","MAX2", "MAX3")
df <- cbind(df, maxes)
Now I need to create a new column (call ir m_sum) that has the sum values of MAX1 and MAX2.
For example, for ID=1, MAX1 = x2 and MAX2 = x4, then m_sum the shold be equal to 1.2240818 + 0.42646422 = 1.650546.