I have the following data frame:
mydf <- data.frame(label = c("A", "B", "C"),
Var1 = c(0.07635660, 0.22186266, -0.13299621),
Var2 = c(0.25517996, 0.65896751, 0.32703359),
Var3 = c(0.63174426, 0.21518955, 0.47102852))
And for each row, I want to add a new variable that would return the name of the variable for which it has the maximum value:
mydf_end_goal <- data.frame(label = c("A", "B", "C"),
Var1 = c(0.07635660, 0.22186266, -0.13299621),
Var2 = c(0.25517996, 0.65896751, 0.32703359),
Var3 = c(0.63174426, 0.21518955, 0.47102852),
Max = c("Var3", "Var2", "Var3"))
What would be the most efficient way of doing this, preferably using dplyr
or purrr
? Right now, the best I can come up with is a series of ifelse
conditions, which gets really annoying as I have more variables than in my toy example above:
mydf %>%
rowwise() %>%
mutate(Max = ifelse(Var1 > Var2 & Var1 > Var3, "Var1",
ifelse(Var2 > Var1 & Var2 > Var3, "Var2", "Var3")))