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

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


On Sep 19, 2011, at 12:19 PM, Marc Schwartz wrote:

> 
> On Sep 19, 2011, at 12:14 PM, Marc Schwartz wrote:
> 
>> 
>> 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.

Rich,

One more time…re-post:

I forgot a critical '!' in both. They should be

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

and the DF approach is not correct either. It should be:

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

I clearly have not had enough caffeine yet today…

Marc



More information about the R-help mailing list