# Inconsistent results from attempting to count matching values in lists within data frames in R

Pretty new to R and to stackexchange. My apologies if I make any faux pas.

I'm working with the North Carolina births data from irss.unc.edu. I have a data frame NCBIRTH1450 with several lists, all of which have numeric or NA values. I'm just trying to calculate some proportions from the data. For one of the lists, I'm able to get the proportion of male babies by using

pBoyBaby <- sum(SEX==1)/sum(!is.na(SEX))


The above returns a numeric value for pBoyBaby. My aim is to take the number of matching values and divide by the number of non-missing values, and I get an appropriate result.

However, when I attempt the same function on another column of the same type,

pMomSmokes <- sum(SMOKE==1)/sum(!is.na(SMOKE))


The object pMomSmokes takes the value NA. How could I identify the difference between the natures of SMOKE and SEX, for instance, that explain this discrepancy? I really don't know why it's doing this.

In case it's relevant, SEX has values of either 1 or 2 -- with no NA's -- while SMOKE has the values 0 or 1, with a few NA's.

Thanks!

-
 probably should be moved to Stack Overflow? – Peter Ellis Apr 10 '12 at 19:10 @Peter IMHO, this is a border case: handling of NA/Null/Nodata values is of general interest in statistical analysis. There appear to be some lessons in this example that apply beyond only R applications (even though at present that good stuff is found primarily in comments :-). – whuber♦ Apr 10 '12 at 19:38

pMomSmokes <- sum(SMOKE==1, na.rm=T)/sum(!is.na(SMOKE))


The issue is the sum() function - read the help file ?sum to see why. If there are any NAs in the vector fed to sum it returns an NA. When an element in SMOKE is NA then that same element of SMOKE==1 will also be NA (because if R doesn't know what SMOKE is, it also doesn't know if it equals 1).

More generally you might want to look at the functions table() and prop.table().

Also, with R you need to be careful about what you call a "list" - you actually have vectors I think. A list is something a bit different. See the very useful Quick-R site for a good explanation, or any of the introductory material on CRAN.

-
Thanks Peter. I take it the == does not exclude NAs? – dubhousing Apr 10 '12 at 19:14
Yes. The issue is the sum() function - read the help file ?sum to see why. If there are any NAs in the vector fed to sum it returns an NA. When an element in SMOKE is NA then that same element of SMOKE==1 will also be NA (because if R doesn't know what SMOKE is, it also doesn't know if it equals 1). – Peter Ellis Apr 10 '12 at 19:17
@dubhousing: == is not doing what you think it's doing. Try doing SMOKE==1 and see what the result is. As Peter Ellis says, the == is doing a reasonable thing -- not what you're thinking it does, though -- and the issue arises in sum. That's a helpful tip: if a statement isn't working as you expect, try the individual pieces to see where perhaps it's working differently. – Wayne Apr 10 '12 at 21:23
Thanks very much Wayne. I wasn't certain as to how reducible the components of code were but based on your suggestion I can see how valuable that will be. – dubhousing Apr 10 '12 at 22:17