[R] Results of applying na.omit on zoo object

Marc Schwartz marc_schwartz at me.com
Mon Sep 19 19:14:19 CEST 2011


On Sep 19, 2011, at 12:01 PM, Rich Shepard wrote:

> On Mon, 19 Sep 2011, Marc Schwartz wrote:
> 
>> You can look at ?complete.cases for one approach, presuming that it will
>> work on zoo objects.
> 
> Marc,
> 
>  That's the opposite of what I want. It returns only rows with no missing
> data. I'm looking for something that will return rows with _only_ missing
> data, and drop them in the bit bucket.
> 
> Thanks,
> 
> Rich


Rich,

OK, I mis-read your post.

Depending upon the underlying structure/class of the zoo object (eg. matrix versus data frame) and presuming that there are no functions in zoo that provide this specific functionality, you may have to create a function that goes row-by-row looking for all NA's.

Possibly something along the lines of the following for a matrix:

  zoo.object[apply(zoo.object, 1, function(x) all(is.na(x))), ]


That won't work for a data frame class object, so you might have to loop over the rows with a for() loop or sapply():

  zoo.object[sapply(rownames(zoo.object), function(x) all(is.na(x))), ]


Both of the above are untested.

HTH,

Marc

P.S. I completely forgot that today is International Talk Like A Pirate Day, so Rrrrrr Matey…  ;-)



More information about the R-help mailing list