[R] names of columns

arun smartpink111 at yahoo.com
Thu Jan 16 22:03:34 CET 2014


I guess, you could also do:
names(dat1)[max.col(dat1)]
#[1] "a" "b"
A.K.




On Thursday, January 16, 2014 3:47 PM, arun <smartpink111 at yahoo.com> wrote:
Try:
dat1 <- read.table(text="a     b     c     d
1  0.5    0.1  0.2   0.2
5  0.3    0.5  0.1   0.1",sep="",header=TRUE)
 data.frame(Names=apply(dat1,1,function(x) names(x)[x %in% max(x)]))
#  Names
#1     a
#5     b

#or
colnames(dat1)[apply(dat1,1,which.max)]
#[1] "a" "b"


A.K.


Hi, 

I need a small help... 

If I have a data frame like 
      a     b     c     d 
1  0.5    0.1  0.2   0.2 
5  0.3    0.5  0.1   0.1 

I need the name of the column with the biggest number in each column. My results will be 

1  a 
5  b 

If I do apply(data.frame, 1, max) I have the maximum by row but I want the name, not the value... 

Thanks




More information about the R-help mailing list