[R] Learning to speak R: simple data processing

ej ejohnso9 at earthlink.net
Fri Jan 11 00:59:29 CET 2013


So, I am just trying to learn R...

Here is a rather contrived example that would be pretty obvious to me in
terms of writing code to loop through elements, but the slick, fast, compact
way of expressing this in R is not obvious to me.

Here's code to generate a simple matrix of data:

> m <- floor(matrix(runif(24, 1, 100), 8, 3))
> m
     [,1] [,2] [,3]
[1,]   75    5   15
[2,]   82   79    2
[3,]   72   24   55
[4,]   88   38    3
[5,]   42   82   98
[6,]   38   78   43
[7,]   79   88   60
[8,]    6   89   43


I see how I can get the max on each row as:

> apply(m, 1, max)
[1] 75 82 72 88 98 78 88 89

I want to generate a vector with the column position of each row max. 

The whole vector for the matrix shown above would be:
1 1 1 1 3 2 2 2

Is there some easy, straightforward way to compute such a thing short of
looping over the rows and columns of the matrix?

Thanks,
-ej





--
View this message in context: http://r.789695.n4.nabble.com/Learning-to-speak-R-simple-data-processing-tp4655194.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list