Hi, Hopefully someone can point me in the right direction on how I would go about solving the following. I have some data and need to find the column name of the maximum value in each row. This could be the data...> a <- data.frame(x = rnorm(4), y = rnorm(4), z = rnorm(4)) > ax y z 1 1.6534561 0.11523404 0.2261730 2 -1.2274320 -0.24096054 1.5096028 3 -1.4503096 0.07227427 1.6740867 4 0.1867416 1.25318913 -0.7350560 Here is what I need to generate... 1 x 2 z 3 z 4 y Any pointers would be appreciated. Regards, -- View this message in context: http://r.789695.n4.nabble.com/finding-max-value-in-a-row-and-reporting-colum-name-tp2309358p2309358.html Sent from the R help mailing list archive at Nabble.com.
Ilaria Prosdocimi
2010-Aug-01 12:12 UTC
[R] finding max value in a row and reporting colum name
pdb <philb <at> philbrierley.com> writes:> > > Hi, >> Here is what I need to generate... > > 1 x > 2 z > 3 z > 4 y > > Any pointers would be appreciated. > > Regards, >this should work, probably other options are possible a <- data.frame(x = rnorm(4), y = rnorm(4), z = rnorm(4)) z<-apply(a,1,which.max) names(a)[z]
Dennis Murphy
2010-Aug-01 12:26 UTC
[R] finding max value in a row and reporting colum name
a <- data.frame(x = rnorm(4), y = rnorm(4), z = rnorm(4))> names(a)[apply(a, 1, which.max)][1] "y" "z" "z" "y"> ax y z 1 -0.8839957 -0.8824065 -0.9343157 2 0.3918695 1.4246880 1.6401349 3 -0.4020719 0.1342691 0.8041808 4 0.1500775 0.8966310 -0.2204660 HTH, Dennis On Sun, Aug 1, 2010 at 3:00 AM, pdb <philb@philbrierley.com> wrote:> > Hi, > > Hopefully someone can point me in the right direction on how I would go > about solving the following. > > I have some data and need to find the column name of the maximum value in > each row. > > This could be the data... > > > a <- data.frame(x = rnorm(4), y = rnorm(4), z = rnorm(4)) > > a > x y z > 1 1.6534561 0.11523404 0.2261730 > 2 -1.2274320 -0.24096054 1.5096028 > 3 -1.4503096 0.07227427 1.6740867 > 4 0.1867416 1.25318913 -0.7350560 > > Here is what I need to generate... > > 1 x > 2 z > 3 z > 4 y > > Any pointers would be appreciated. > > Regards, > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/finding-max-value-in-a-row-and-reporting-colum-name-tp2309358p2309358.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
pdb wrote:> > Hi, > > Hopefully someone can point me in the right direction on how I would go > about solving the following. > > I have some data and need to find the column name of the maximum value in > each row. > > This could be the data... > >> a <- data.frame(x = rnorm(4), y = rnorm(4), z = rnorm(4)) >> a > x y z > 1 1.6534561 0.11523404 0.2261730 > 2 -1.2274320 -0.24096054 1.5096028 > 3 -1.4503096 0.07227427 1.6740867 > 4 0.1867416 1.25318913 -0.7350560 > > Here is what I need to generate... > > 1 x > 2 z > 3 z > 4 y > > Any pointers would be appreciated. > > Regards, > > > >as.data.frame(cbind(row.names(a),apply(a,1,function(x) names(a)[which(x==max(x))]))) LB -- View this message in context: http://r.789695.n4.nabble.com/finding-max-value-in-a-row-and-reporting-colum-name-tp2309358p2310192.html Sent from the R help mailing list archive at Nabble.com.