My question may not be so well formulated and sounds complicated. But what I want is very simple. From the given Data frame Df I have made a new Data frame Df1 which contains the max values from each individual column. Now I want to add to my new data frame Df1 a column with the name of each column from which the max value comes.
I have prepared an example for this.
A <- runif(5,min = 0.1, max = 3)
B <- runif(5,min = 0.1, max = 3)
C <- runif(5,min = 0.1, max = 3)
D <- runif(5,min = 0.1, max = 3)
E <- runif(5,min = 0.1, max = 3)
F <- runif(5,min = 0.1, max = 3)
Car <- c("Audi","BMW","Mercedes","Ford","Opel")
Df<-data.frame(Car,A,B,C,D,E,F)
Df1<-Df%>%
group_by(Car)%>%
summarize(max.value = max(A,B,C,D,E,F))
Df:
Df1:
What I want:
An option would be
library(dplyr)
Df %>%
transmute(Car, max.value = pmax(!!! .[-1]),
rowName = names(.)[-1][max.col(.[-1])])