Gary Dong
2013-Nov-01 17:03 UTC
[R] find max value in each row and return column number and column name
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))> ax y z 1 -0.7289964 0.2194702 -2.4674780 2 1.0889353 0.3167629 -0.9208548 3 -0.6374692 -1.7249049 0.6567313 4 -0.1348642 0.4507473 -1.7309010 In this data frame, I compare y and z only. What I need: x y z max max.col.num max.col.name 1 -0.7289964 0.2194702 -2.4674780 0.2194702 2 y 2 1.0889353 0.3167629 -0.9208548 0.3167629 2 y 3 -0.6374692 -1.7249049 0.6567313 0.6567313 3 z 4 -0.1348642 0.4507473 -1.7309010 0.4507473 2 y Any suggestion will be greatly appreciated! Thank you! Gary [[alternative HTML version deleted]]
Clint Bowman
2013-Nov-01 17:34 UTC
[R] find max value in each row and return column number and column name
?which.max should start you down the right path Clint Bowman INTERNET: clint at ecy.wa.gov Air Quality Modeler INTERNET: clint at math.utah.edu Department of Ecology VOICE: (360) 407-6815 PO Box 47600 FAX: (360) 407-7534 Olympia, WA 98504-7600 USPS: PO Box 47600, Olympia, WA 98504-7600 Parcels: 300 Desmond Drive, Lacey, WA 98503-1274 On Fri, 1 Nov 2013, 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.7289964 0.2194702 -2.4674780 > 2 1.0889353 0.3167629 -0.9208548 > 3 -0.6374692 -1.7249049 0.6567313 > 4 -0.1348642 0.4507473 -1.7309010 > > In this data frame, I compare y and z only. > > What I need: > > x y z > max max.col.num max.col.name > 1 -0.7289964 0.2194702 -2.4674780 0.2194702 2 > y > 2 1.0889353 0.3167629 -0.9208548 0.3167629 2 > y > 3 -0.6374692 -1.7249049 0.6567313 0.6567313 3 > z > 4 -0.1348642 0.4507473 -1.7309010 0.4507473 2 > y > > > Any suggestion will be greatly appreciated! > > Thank you! > > Gary > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
arun
2013-Nov-01 17:39 UTC
[R] find max value in each row and return column number and column name
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 <pdxgary163 at gmail.com> 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.7289964? 0.2194702 -2.4674780 2? 1.0889353? 0.3167629 -0.9208548 3 -0.6374692 -1.7249049? 0.6567313 4 -0.1348642? 0.4507473 -1.7309010 In this data frame, I compare y and z only. What I need: ? ? ? ? ? ? x? ? ? ? ? ? ? ? ? ? y? ? ? ? ? ? ? ? ? ? z max? ? ? ? ? ? ? ? max.col.num? ? ? ? max.col.name 1 -0.7289964? 0.2194702 -2.4674780? ? ? ? 0.2194702? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? ? ? ? y 2? 1.0889353? 0.3167629 -0.9208548? ? ? ? 0.3167629? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? ? ? ? y 3 -0.6374692 -1.7249049? 0.6567313? ? ? ? 0.6567313? ? ? ? ? ? ? 3 ? ? ? ? ? ? ? ? ? ? z 4 -0.1348642? 0.4507473 -1.7309010? ? ? ? 0.4507473? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? ? ? ? y Any suggestion will be greatly appreciated! Thank you! Gary ??? [[alternative HTML version deleted]] ______________________________________________ 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.
David Winsemius
2013-Nov-01 18:01 UTC
[R] find max value in each row and return column number and column name
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.7289964 0.2194702 -2.4674780 > 2 1.0889353 0.3167629 -0.9208548 > 3 -0.6374692 -1.7249049 0.6567313 > 4 -0.1348642 0.4507473 -1.7309010 > > In this data frame, I compare y and z only. > > What I need: > > x y z max max.col.num max.col.name > 1 -0.7289964 0.2194702 -2.4674780 0.2194702 2 > y > 2 1.0889353 0.3167629 -0.9208548 0.3167629 2 > y > 3 -0.6374692 -1.7249049 0.6567313 0.6567313 3 > z > 4 -0.1348642 0.4507473 -1.7309010 0.4507473 2 > y > > > Any suggestion will be greatly appreciated!cbind(a, max=apply(a,1,max), max.col.num =apply(a,1,which.max) , max.col.name= names(a)[apply(a,1,which.max)] )> > Thank you! > > Gary > > [[alternative HTML version deleted]]You can express your appreciation by posting in plain-text in the future.> > ______________________________________________ > 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.David Winsemius Alameda, CA, USA