Re: [R] creating additional column

From: Philipp Pagel <p.pagel_at_wzw.tum.de>
Date: Tue, 08 Mar 2011 13:38:47 +0100

        Hi!

max.col does what you want. Example:

> dat <- data.frame(a=rnorm(20),b=rnorm(20),c=rnorm(20))
> dat

             a b c

1   1.17910304 -0.56951219 -0.2243664
2  -1.43840866 -0.99013855 -0.1613536
3   1.08515152 -0.77975274  0.3734530
4  -0.92154605 -0.20318367  0.1384842

[...]
> dat$maxcol <- colnames(dat)[max.col(dat)]
> dat
             a           b          c maxcol
1   1.17910304 -0.56951219 -0.2243664      a
2  -1.43840866 -0.99013855 -0.1613536      c
3   1.08515152 -0.77975274  0.3734530      a
4  -0.92154605 -0.20318367  0.1384842      c
[...]

cu

        Philipp

On Tue, Mar 08, 2011 at 01:25:10PM +0100, Bodnar Laszlo EB_HU wrote:
> Hello everybody,
>
> I have a little problem in good old R. It is basically the following.
>
> I have this small database with 3 rows and the following columns:
> d1,
> d2,
> d3 and
> Highest d value - which selects the highest value from d1, d2, d3 in each row.
>
> d1 d2 d3 Highest d value
> 1 51.398426 39.111721 11.6086220 51.398426
> 2 4.057801 7.728407 0.1234711 7.728407
> 3 7.279341 7.360509 18.2964676 18.296468
>
> I'd like to make an additional column which shows the label of the relevant column where we've found the maximum d value. Something like this:
>
> d1 d2 d3 Highest d value Where is the maximum?
> 1 51.398426 39.111721 11.6086220 51.398426 d1
> 2 4.057801 7.728407 0.1234711 7.728407 d2
> 3 7.279341 7.360509 18.2964676 18.296468 d3
>
> Is there an easy way to do this?
> Thank you very much and have a pleasant day!
>
> Laszlo
>
> ____________________________________________________________________________________________________
> Ez az e-mail ??s az ??sszes hozz?? tartoz?? csatolt mell??klet titkos ??s/vagy jogilag, szakmailag vagy m??s m??don v??dett inform??ci??t tartalmazhat. Amennyiben nem ??n a lev??l c??mzettje akkor a lev??l tartalm??nak k??zl??se, reproduk??l??sa, m??sol??sa, vagy egy??b m??s ??ton t??rt??n?? terjeszt??se, felhaszn??l??sa szigor??an tilos. Amennyiben t??ved??sb??l kapta meg ezt az ??zenetet k??rj??k azonnal ??rtes??tse az ??zenet k??ld??j??t. Az Erste Bank Hungary Zrt. (EBH) nem v??llal felel??ss??get az inform??ci?? teljes ??s pontos - c??mzett(ek)hez t??rt??n?? - eljuttat??s????rt, valamint semmilyen k??s??s??rt, kapcsolat megszakad??sb??l ered?? hib????rt, vagy az inform??ci?? felhaszn??l??s??b??l vagy annak megb??zhatatlans??g??b??l ered?? k??r??rt.
>
> Az ??zenetek EBH-n k??v??li k??ld??je vagy c??mzettje tudom??sul veszi ??s hozz??j??rul, hogy az ??zenetekhez m??s banki alkalmazott is hozz??f??rhet az EBH folytonos munkamenet??nek biztos??t??sa ??rdek??ben.
>
>
> This e-mail and any attached files are confidential an...{{dropped:19}}



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. Received on Tue 08 Mar 2011 - 12:41:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Mar 2011 - 17:20:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive