[R] Skip last NA's?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Mar 2 13:58:28 CET 2006

Do you mean you wish to create a vector without the trailing NAs
but with the others?  If so, try:

head(x, length(na.locf(x, rev = TRUE)))


head(x, length(na.locf(rev(x))))

On 3/2/06, Robert Lundqvist <Robert.Lundqvist at ltu.se> wrote:
> I wonder if anyone could help me find an expression for skipping the last
> missing values in a vector? The kind of material I have is something like
> x<-c(23,12,NA,23,24,21,NA,NA,NA)
> I would like to skip the last NA's, but not the ones in between other
> vallues. Any hints? (Why not do this by simply take x[1:6]? I have several
> vectors a couple of thousand observations long with varying numbers of
> NA's in the end. I'd prefer not to search through all of these one at a
> time.)
> Robert
> ______________________________________________
> 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