Problems with NA

I have a dataset 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 consider NA:

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

which gives

> isna
   [1] 2

I do not understand why the following does not work:

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

It should be 6.

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

It should be 2.

I can make the following work:

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

My dataset is quite large, so I need to know what I'm doing, and I don't understand the difference between the two expressions.

Any help is greatly appreciated.

+4
source share
2 answers

Remove NA, the following code will help you:

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

Or it can be done as follows:

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

if this wiil is not working correctly explain your problem

+5
source

, , , (NA) - (1 )., NA==1, FALSE, NA. :

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

:

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

All Articles