Let's say that I have a df contains ID, gender, and several numerical variables. See below
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)
The goal is to create two columns: Max1 and Max2, where
MAX1 is the variable name of the largest max of (x1,x2,x3,x4,x5).
MAX2 is the variable name of the second largest max of (x1,x2,x3,x4,x5)
So I need to find MAX1 and MAX2 for each row in df
EX: for ID=1, MAX1 = "x2" and MAX2 = "x4"