# [R] Problem with na.omit when using length()

Viju Moses vijumoses at gmail.com
Tue Oct 6 06:51:40 CEST 2009

```I'm seeing what looks to me like odd behaviour when I use na.omit on a
simple "length" function, as follows.

> sno
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31 32 33 34
> a
[1] 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
> b
[1]  1  1  0  1  1  1  0  0 NA  0  0  0 NA  0  1 NA  0  1  0  0  0  0
NA  0  0  0  0 NA  0 NA  0  1  0  0

#NA refers to no data available.

> df=data.frame(sno,a,b)
# I'm pasting the sorted data frame below:
> sortdf=df[order(a,b),]
> sortdf
sno a  b
3    3 0  0
7    7 0  0
8    8 0  0
10  10 0  0
11  11 0  0
12  12 0  0
14  14 0  0
17  17 0  0
20  20 0  0
21  21 0  0
22  22 0  0
24  24 0  0
25  25 0  0
26  26 0  0
27  27 0  0
29  29 0  0
31  31 0  0
33  33 0  0
34  34 0  0
1    1 0  1
4    4 0  1
9    9 0 NA
13  13 0 NA
23  23 0 NA
28  28 0 NA
30  30 0 NA
19  19 1  0
2    2 1  1
5    5 1  1
6    6 1  1
15  15 1  1
18  18 1  1
32  32 1  1
16  16 1 NA

#Now I wish to count howmany records have a=1 AND b=0. From the lower
section of that sorted dataframe we see the answer is 1 (record # 19).
But instead I'm seeing 2. Probably counting record # 16 also.

> na.omit(length(sno[a==1 & b==0]))
[1] 2

I'd be grateful to anyone who can point out what I'm doing wrong.

Regards.

```