[R] how to count number of elements in a vector that are not NA ?
Godmar Back
godmar at gmail.com
Tue Jul 7 21:37:27 CEST 2009
Thank you for the many replies! This is really a very friendly and
helpful community!
On Tue, Jul 7, 2009 at 3:06 PM, Henrique Dallazuanna<wwwhsd at gmail.com> wrote:
> another option should be:
>
> length(na.omit(v))
>
I think the above is what I was looking for since, presumably, it uses
the very same test as other methods that exploit na.*, such as
cor.test.
... so I tried it out and, of course, R's typing trips me over:
> length(na.omit(q[[51]]))
[1] 1
Why? I think I know: q[[51]] is a data frame with 1 component, and
'length' applied to a data frame counts its components. Not a
problem, I can do:
> length(na.omit(q[[51]][,1]))
[1] 110
or
> length(na.omit(q[[51]])[,1])
[1] 110
for this data frame of 1 components.
Do other functions act like length()? Let's look at sum():
> sum(na.omit(q[[51]]))
[1] 132
> sum(na.omit(q[[51]][,1]))
[1] 132
> sum(na.omit(q[[51]])[,1])
[1] 132
Nope.
Easy to get wrong. I buy the last two, but the first? I assume sum()
applied to a data frame reduces all its elements across all
components(?). Letting the call "sum(na.omit(q[[51]]))" fail would
have been a sensible design decision too, IMO. One needs to be really
careful in R.
Thank you again,
- Godmar
More information about the R-help
mailing list