[R] Variable which has the maximum value of DF

jim holtman jholtman at gmail.com
Mon Oct 15 17:52:43 CEST 2007


You can also use arr.ind=TRUE to get the indices of the maximum value:

> Lines <- "var1 var2 var3 var4 var5 var6
+
+  0      2      1     2      0      0
+  2      3      7     6      0      1
+  1.5    4      9     9      6      0
+  1.0    6      10    22     3      3
+ "
> DF <- read.table(textConnection(Lines), header=TRUE)
> DF
  var1 var2 var3 var4 var5 var6
1  0.0    2    1    2    0    0
2  2.0    3    7    6    0    1
3  1.5    4    9    9    6    0
4  1.0    6   10   22    3    3
> which(DF == max(DF), arr.ind=TRUE)
     row col
[1,]   4   4
>


On 10/15/07, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:
> Hi,
>
> Suppose I have a data.frame like this
>
> Lines <- "var1 var2 var3 var4 var5 var6
>
>  0      2      1     2      0      0
>  2      3      7     6      0      1
>  1.5    4      9     9      6      0
>  1.0    6      10    22     3      3
> "
> DF <- read.table(textConnection(Lines), skip=1)
> names(DF) <- scan(textConnection(Lines), what = "", nlines = 1)
>
> How do I find the name of the variable which has the maximum value of
> the data.frame? In this example the answer is "var3" (22).
>
> Best regards,
> Lauri
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list