[R] Fw: length() misbehaving?: More

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri Mar 14 17:01:29 CET 2003

David Parkhurst wrote:
> With the problem below, I've discoved that
> n95trt<-length(byyr$cnd95[byyr$cnd95=="tr"&!is.na(byyr$cnd95)])
> does give me the correct count for the number of "tr" entries.  (The same behavior
> occurs for the "c" level of the cnd95 factor.)  It appears that
> byyr$cnd95=="tr"
> is finding both "tr" AND NA entries.  Is this a bug, or is it to be expected?

This is expected - it's an inappropriate use of length(), see my earlier 
Calculations, comparisons and indexing with NA returns NA. That's 
documented and therefore expected.

Uwe Ligges

> Dave
> Subject: length() misbehaving?
>>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"])
>>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.)
>>Thanks for any help.
>>Dave Parkhurst
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

More information about the R-help mailing list