[R] select element from each row of the matrix

David Winsemius dwinsemius at comcast.net
Wed Jul 20 22:34:54 CEST 2011


On Jul 20, 2011, at 4:23 PM, gallon li wrote:

> I have a 5 column matrix like
>
> 12 10 8 6 3
> 10 9 8 7 5
> 14 NA 4 NA NA NA
> 15 NA 10 NA 5
> ...

Probably something along the lines of

aapply(mtx, 1, function(x) { c( x[ which(x <= 5)[1] ], # first row are  
the values
                                 which(x <= 5)[1])  } ) # second row  
the positions

-- 
David.

>
> I want to select the position of the first entry for each row <=5
>
> for example, for the first row, I want to select the last element  
> and return
> its position as 5;
> for th e third row, I want to select the third element and return its
> position as 3;
> similarly for the 4th row, I want to select the fifth element and  
> return its
> position 5.
>
> I am wondering how to do this fast? Thanks a lot!
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list