I am trying to find the largest column value and the second largest column value and the names of both columns. I'm struggling to get the second largest column name however.
I tried to write an lapply function that removed the value of the first max from consideration, but it threw off the column name count. Any suggestions?
temp<-data.frame(c(1,2,3,4),c(1,2,3,1),c(4,5,1,2),c(1,6,5,4),c(2,2,2,2))
colnames(temp)<-c("c1","c2","c3","c4","c5")
temp$MaxOrders<-as.numeric(apply(temp[,c(-1)],1,function(x){x[which.max(x)]}))
temp$secondMaxOrders<-as.numeric(apply(temp[,c(2,3,4,5)],1,function(x){x[order(x)[2]]}))
temp$MaxColName<-colnames(temp)[c(-1)][max.col(temp[,c(-1)],ties.method="first")]
temp
c1 c2 c3 c4 c5 MaxOrders secondMaxOrders MaxColName
1 1 1 4 1 2 4 1 c3
2 2 2 5 6 2 6 5 c4
3 3 3 1 5 2 5 3 c4
4 4 1 2 4 2 4 2 c4
GOAL: find the second highest by column name
c1 c2 c3 c4 c5 MaxOrders secondMaxOrders MaxColName secondMaxColumnName
1 1 1 4 1 2 4 2 c3 c5
2 2 2 5 6 2 6 5 c4 c3
3 3 3 1 5 2 5 3 c4 c2
4 4 1 2 4 2 4 2 c4 c3