[R] find position to last number in a vector

apjaworski at mmm.com apjaworski at mmm.com
Fri May 26 20:56:53 CEST 2006


I am sure there are several ways of doing this but how about something like
this:

y <- c(NA,NA,10,NA,2,NA,NA,1,NA,NA)
x <- c(10,20,30,40,50,60,70,80,85,100)

tail(x[!is.na(y)], 1)

or

rev(x[!is.na(y)])[1]

Rudimentary checks indicate that the second expression is much faster than
the first.  Also, if y is contains NAs only, the first expression returns
numeric(0) and the second one NA.

Hope this helps,

Andy

__________________________________
Andy Jaworski
518-1-01
Process Laboratory
3M Corporate Research Laboratory
-----
E-mail: apjaworski at mmm.com
Tel:  (651) 733-6092
Fax:  (651) 736-3122


                                                                           
             Anders                                                        
             Bjørgesæter                                                   
             <anders.bjorgesat                                          To 
             er at bio.uio.no>            r-help at stat.math.ethz.ch            
             Sent by:                                                   cc 
             r-help-bounces at st                                             
             at.math.ethz.ch                                       Subject 
                                       [R] find position to last number in 
                                       a vector                            
             05/26/2006 01:30                                              
             PM                                                            
                                                                           
                                                                           
                                                                           
                                                                           




Hello

Is there a function to extract the position (i.e. row number or more
desirable the value from another column with the same rownumber ) of
last number in a vector? This is done in a loop with 1000s of columns.

e.g.

vector/column.n: na,na,10,na,2,na,na,1,na,na
fixed column:10,20,30,40,50,60,70,80,85,100

result: 8 and/or 80

Thanks...

Best Regards
Anders

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html



More information about the R-help mailing list