On Jul 18, 2010, at 12:02 PM, stephen sefick wrote:

I am confused by the behavior of the below piece of code.  The NAs are
making it past the logical call ==0.  I am sure that I am missing
something.  I just don't understand this behavior.  Thanks for your
help in advance.


########code#######################################################
left <- (structure(list(measurment_num = c(2, 2.2, 2.4, 2.6, 2.8, 2.82,
3, NA, NA, NA), bankfull_depths_m = c(1.29, 1.28, 1.23, 0.18,
-0.05, 0, -0.09, NA, NA, NA)), .Names = c("measurment_num", "bankfull_depths_m"
), row.names = c(10L, 11L, 12L, 13L, 14L, 20L, 15L, 16L, 17L,
18L), class = "data.frame"))

if(sum(left[,"bankfull_depths_m"]==0, na.rm=TRUE) == 1){
left_min <- left[left[,"bankfull_depths_m"]==0, "measurment_num"]
}

left

Why aren't you looking at left_min? That is what the code is supposed to be changing. I do not get an error and left_min[1] (now) =='s 2.82.

Regarding why there are the 3 NA's, ... you need to look at the documentation for Extract in the section very appropriately entitled:
"NAs in indexing"

I have been bitten by that behavior more times than I care to admit and I am not the only person that thinks that aspect of the R language is badly designed. See Aniko Szabo's comments:

http://radfordneal.wordpress.com/2008/09/21/design-flaws-in-r-3-%E2%80%94-zero-subscripts/

The problem is in data.frame[ and any NA in a logical vector will return a row of NA's. This can be avoid by wrapping which() around the logical vector which seems entirely wasteful or using subset().


David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to