r - Select max or equal value from several columns in a data frame -
i'm trying select column highest value each row in data.frame. instance, data set such.
> df <- data.frame(one = c(0:6), 2 = c(6:0)) > df 1 2 1 0 6 2 1 5 3 2 4 4 3 3 5 4 2 6 5 1 7 6 0
then i'd set column based on rows. data frame this.
> df 1 2 rank 1 0 6 2 2 1 5 2 3 2 4 2 4 3 3 3 5 4 2 1 6 5 1 1 7 6 0 1
i imagine there sort of way can use plyr or sapply here it's eluding me @ moment.
there might more efficient solution,
ranks <- apply(df, 1, which.max) ranks[which(df[, 1] == df[, 2])] <- 3
edit: spaced!
Comments
Post a Comment