[R] creating additional column

Philipp Pagel p.pagel at wzw.tum.de
Tue Mar 8 13:38:47 CET 2011



	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}}



More information about the R-help mailing list