I'm writing a function that computes a new column based on several fields in my data frame. Part of the process is to do a pre-check on the range of one the input fields before actual processing. Weird thing is that my pre-check always returns the same value, that is, the outcome of the pre-check for the first row of the dataframe.
See example below with a simplified version of my function, which only includes the check. Even though the variable is properly passed to the function, and is different every time, the outcome of the pre-check stays the same. What am I missing?
> my_function = function(x){
+ print(paste(x, x < 32 && x > 0))
+ if (x < 32 && x > 0) {
+ # do_something ...
+ }
+ }
>
> df = data.frame("A" = c(10,20,30,40), "B" = c(0,10,20,30), "C" = c(40,30,20,10))
> my_function(df$A)
[1] "10 TRUE" "20 TRUE" "30 TRUE" "40 TRUE"
> my_function(df$B)
[1] "0 FALSE" "10 FALSE" "20 FALSE" "30 FALSE"
> my_function(df$C)
[1] "40 FALSE" "30 FALSE" "20 FALSE" "10 FALSE"