stephen sefick
2010-Jul-18 16:02 UTC
[R] NA preserved in logical call - I don't understand this behavior because NA is not equal to 0
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 ################################################################## -- Stephen Sefick ____________________________________ | Auburn University? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | Department of Biological Sciences? ? ? ? ?? | | 331 Funchess Hall? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | Auburn, Alabama? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | 36849? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | |___________________________________| | sas0025 at auburn.edu? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | http://www.auburn.edu/~sas0025? ? ? ? ? ?? | |___________________________________| Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods.? We are mammals, and have not exhausted the annoying little problems of being mammals. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -K. Mullis
Uwe Ligges
2010-Jul-18 17:00 UTC
[R] NA preserved in logical call - I don't understand this behavior because NA is not equal to 0
On 18.07.2010 18:02, 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"] > }NA == 0 is ??? --> NA! Use is.na() to check for missing values. Uwe Ligges> left > ################################################################## >
David Winsemius
2010-Jul-18 17:10 UTC
[R] NA preserved in logical call - I don't understand this behavior because NA is not equal to 0
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"] > } > > leftWhy 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