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

David Winsemius dwinsemius at comcast.net
Sun Apr 26 15:47:39 CEST 2009


On Apr 26, 2009, at 8:02 AM, Alessandro wrote:

> Dear User,
> thank for the attention. I have a data.frame with 5 columns (ex:ID,
> a1,a2,a3,a4) and 1000 rows. I wish to find the absolute max value  
> for all
> data.frame and save a new data.frame with the row where is that  
> value. Ex:
>
> ID: 1,2,3,4,5,6,7,8,9,10
>
> a1:1,2,3,4,5,6,7,8,9,10
>
> a2:11,12,13,14,15,16,17,18,19,20
>
> a3:21,22,23,24,25,26,27,28,29,30
>
> a4:31,32,33,34,35,36,37,38,39,40
>
> The max value in the four columns (a1,a2,a3,a4) is 40. The new  
> data.frame is
>
>
> ID:10
>
> A1:10
>
> A2:20
>
> A3:30
>
> A4 :40
>

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

apply(df, 2, max)

# If you want the names to be as specified, then look at the colnames  
function, but at this point I am concerned that I may have already  
done too much of you homework.
--output---
ID a1 a2 a3 a4
10 10 20 30 40
----

  max(df)

[1] 40

-- 


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list