I am trying to write some code which identifies the greatest two values for each row and provides their column number and value.
df = data.frame( car = c (2,1,1,1,0), bus = c (0,2,0,1,0),
walk = c (0,3,2,0,0), bike = c(0,4,0,0,1))
I've managed to get it to do this for the maximum value using the max
and max.col
functions.
df$max = max.col(df,ties.method="first")
df$val = apply(df[ ,1:4], 1, max)
As far as I know there are no equivalent functions for the second highest value so doing this has made things a little trickier. Using this code provides the second highest value but (importantly) not in situations with ties. Also it looks risky.
sec.fun <- function (x) {
max( x[x!=max(x)] )
}
df$val2 <- apply(df[ ,1:4], 1, sec.fun)
Ideally the solution would not involve removing any original data and could be used to find the third, fourth... highest value but neither of these are essential requirements.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…