Re: [R] find max value in each row and return column number and column name

2013-11-01 Thread David Winsemius
On Nov 1, 2013, at 10:03 AM, Gary Dong wrote: > Dear R users, > > I wonder how I can use R to identify the max value of each row, the column > number column name: > > For example: > > a <- data.frame(x = rnorm(4), y = rnorm(4), z = rnorm(4)) > >> a > x y z > 1 -0.7

Re: [R] find max value in each row and return column number and column name

2013-11-01 Thread arun
Hi, Try:   cbind(a,do.call(rbind,apply(a,1,function(x) {data.frame(max=max(x), max.col.num=which.max(x), max.col.name=names(a)[which.max(x)],stringsAsFactors=FALSE)}))) ##assuming that unique max for each row. A.K. On Friday, November 1, 2013 1:05 PM, Gary Dong wrote: Dear R users, I wonder

Re: [R] find max value in each row and return column number and column name

2013-11-01 Thread Clint Bowman
?which.max should start you down the right path Clint BowmanINTERNET: cl...@ecy.wa.gov Air Quality Modeler INTERNET: cl...@math.utah.edu Department of Ecology VOICE: (360) 407-6815 PO Box 47600FAX:(360)

[R] find max value in each row and return column number and column name

2013-11-01 Thread Gary Dong
Dear R users, I wonder how I can use R to identify the max value of each row, the column number column name: For example: a <- data.frame(x = rnorm(4), y = rnorm(4), z = rnorm(4)) > a x y z 1 -0.7289964 0.2194702 -2.4674780 2 1.0889353 0.3167629 -0.9208548 3 -0.6