[R] Index (which) of last non-NA data in dataframe
Liaw, Andy
andy_liaw at merck.com
Tue Jun 28 03:31:16 CEST 2005
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 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