[R] finding max value in a row and reporting colum name

Ilaria Prosdocimi durumkip at aol.com
Sun Aug 1 14:12:11 CEST 2010



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]



More information about the R-help mailing list