Skip to content
Prev 389428 / 398503 Next

unexpected behavior in apply

Hello, 

I'm seeing unexpected behavior when using apply() compared to a for loop when a character vector is part of the data subjected to the apply statement. Below, I check whether all non-missing values are <= 3. If I include a character column, apply incorrectly returns TRUE for d3. If I only pass the numeric columns to apply, it is correct for d3. If I use a for loop, it is correct.
+               d2 = c(1,2,3),
+               d3 = c(NA,NA,6))
d1 d2 d3
1  a  1 NA
2  b  2 NA
3  c  3  6
d1    d2    d3 
FALSE  TRUE  TRUE
d2    d3 
 TRUE FALSE
+   print(all(d[!is.na(d[,i]),i] <= 3))
+ }
[1] FALSE
[1] TRUE
[1] FALSE


Finally, if I remove the NA values from d3 and include the character column in apply, it is correct.
+               d2 = c(1,2,3),
+               d3 = c(4,5,6))
d1 d2 d3
1  a  1  4
2  b  2  5
3  c  3  6
d1    d2    d3 
FALSE  TRUE FALSE


Can someone help me understand what's happening?