Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with NAs

Tags:

r

na

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.

like image 604
Sisse Avatar asked Feb 14 '23 11:02

Sisse


1 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

like image 111
jay_phate Avatar answered Feb 24 '23 23:02

jay_phate