Skip to content

Help to select the raw in a data.frame with the max value

1 message · Robert J. Hijmans

#
Hi Ale,

perhaps something like this will work for you:

ID <- c(1,2,3,4,5,6,7,8,9,10)
a1 <- c(1,2,3,4,5,6,7,8,9,10)
a2 <- c(11,12,13,14,15,16,17,18,19,20)
a3 <- c(21,22,23,24,25,26,27,28,29,30)
a4<- c(31,32,33,34,35,36,37,38,39,40)
df <- data.frame(cbind(ID, a1, a2, a3, a4))

# get max by row, exclude first column (ID)
rowmax <- apply(df[,-1], 1, max)

row <- which(rowmax == max(rowmax))
df[row,]

By the way, I think it is better not to post to two lists at the same
time (many people are not subscribed to both, and this will break the
thread).

Robert


On Sun, Apr 26, 2009 at 8:02 PM, Alessandro
<alessandro.montaghi at unifi.it> wrote: