Re: [R] Index (which) of last non-NA data in dataframe

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Tue 28 Jun 2005 - 11:31:16 EST


If you can be sure that the non-NAs are contiguous, then the following should work:

sapply(d, function(x) max(which(!is.na(x))))

Andy

> From: Pierre Lapointe
>
> Hello,
>
>
>
> In a dataframe, I want the index if the last non-NA data. Example:
>
>
>
> d<-data.frame(matrix(c(1,3,4,2,7,8,1,NA,2),3,3,byrow=TRUE))
>
>
>
> gives:
>
> > d
>
> X1 X2 X3
>
> 1 1 3 4
>
> 2 2 7 8
>
> 3 1 NA 2
>
>
>
> I want a vector that gives me 3 2 3
>
>
>
> I know about tail and which, but I don't know how not to
> consider the NA
>
>
>
> e.g.: last<-tail(d,1,na.rm=TRUE) still gives me a NA
>
> > last
>
> X1 X2 X3
>
> 3 1 NA 2
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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
>
>
>



R-help@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 Received on Tue Jun 28 11:36:01 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:03 EST