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
A fairly standard trick for such situations is to use rle(is.na(x)) In your case you want to see if the last value is TRUE. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Robert Lundqvist 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 > > > > >
Hi probably not the best solution but if(tail(is.na(x), n=1)) x[1 : (length(x) - (tail(rle(is.na(x))$lengths, n=1)))] else x shall do what you want. HTH Petr On 2 Mar 2006 at 9:29, Robert Lundqvist wrote: Date sent: Thu, 2 Mar 2006 09:29:49 +0100 (MET) From: Robert Lundqvist <Robert.Lundqvist at ltu.se> To: R-help at stat.math.ethz.ch Subject: [R] Skip last NA's? Send reply to: Robert.Lundqvist at ltu.se <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe> <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>> 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.htmlPetr Pikal petr.pikal at precheza.cz
Do you mean you wish to create a vector without the trailing NAs but with the others? If so, try: library(zoo) head(x, length(na.locf(x, rev = TRUE))) or library(zoo) 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 >
Robert Lundqvist 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 >x[1:max(which(!is.na(x)))] or, to allow for cases in which x may consist entirely of NAs or have length 0, x[0:max(0,which(!is.na(x)))]