[R] length() misbehaving?
Ott Toomet
otoomet at econ.dk
Fri Mar 14 17:20:32 CET 2003
HI Dave,
| From: "David Parkhurst" <parkhurs at indiana.edu>
| Date: Fri, 14 Mar 2003 10:35:19 -0500
|
| I'm having a weird problem with length(), in R1.6.1 under windows2000. I have a
| dataframe called byyr, with ten columns, the first of which is named cnd95.
| summary(byyr) shows that byyr$cnd95 contains the factor level "tr" 66 times. Also,
| when I enter byyr$cnd95 at the command line, I can count 66 "tr" elements in the
| resulting vector. However, when I enter
|
| n95trt <- length(byyr$cnd95[byyr$cnd95=="tr"])
| n95trt
|
| the result is 68! Any ideas why this is happening, and how I can fix the miscount?
| (That column also contains 69 entries of "c", and (relevantly?) two NA's.)
Yes, NA-s are relevant. Try:
> a <- factor(c("a", "a", NA))
> a
[1] a a <NA>
Levels: a
> summary(a)
a NA's
2 1
> a=="a"
[1] TRUE TRUE NA
# there are 3 elements in the vector, hence there is 3 in a[a=="a"] too.
> sum(a=="a", na.rm=T)
[1] 2
will give you the correct length.
perhaps it helps.
Ott
|
| Thanks for any help.
|
| Dave Parkhurst
More information about the R-help
mailing list