Problems with NAs

Viewed 107

I have a data set like

dat
   ejer_id person_alder koen  aar
1        1            9    1 2011
2        2            9    1 2011
3        3            7    1 2011
4        4           94    1 2011
5        5           94    2 2011
6        6           93   NA 2011
7        7           93    1 2011
8        8           91    2 2011
9        9           91    1 2011
10      10           91   NA 2011

I count the NAs:

isna <- sum(is.na(dat$koen))

which gives

> isna
   [1] 2

I don't understand why the following doesn't work:

 > length( dat$koen[dat$koen == 1] )
[1] 8

This should be 6.

length( dat$koen[dat$koen == 2] ) [1] 4

This should be 2.

I can get the following to work:

> length( which( dat$koen == 1 ) )
[1] 6
> length( which( dat$koen == 2 ) )
[1] 2

My data set is quite large, so I need to know, what I am doing, and I don't understand the difference between these two expressions.

Any help is greatly appreciated.

2 Answers

Remove NA's, following code help you:

length(na.omit(dat$koen[dat$koen==1]))

Or same can be done in following way:

length(dat$koen[which(!is.na(dat$koen[dat$koen==1]))])

if this wiil not work, explain your issue properly

The thing you should know is that you cannot compare nothing (NA) with something (1 here)., i.e. when the algorithm does this NA==1, it does NOT return FALSE but it will return NA. That is the reason you are getting:

 > length( dat$koen[dat$koen == 1] )
[1] 8

Try this instead:

 > length(dat$koen[dat$koen==1 & !is.na(dat$koen)])
[1] 6
Related