I have some nonintuitive behaviour in R.
According to the documentation
& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.
Being a new to R, this does not yet trigger a full mental model of what is supposed to happen, but it sounds similar to the 'and' vs 'conditional and' seen in other programming languages (also called Short-Circuiting Logical Operators)
Create a data frame:
mhbins <- data.frame(val=seq(0,10),bin=rep(c(NA),length.out=11))
val bin
1 0 NA
2 1 NA
3 2 NA
4 3 NA
5 4 NA
6 5 NA
7 6 NA
8 7 NA
9 8 NA
10 9 NA
11 10 NA
Then patch it up:
mhbins$bin[1 <= mhbins$val & mhbins$val <= 7] <- "some"
val bin
1 0 <NA>
2 1 some
3 2 some
4 3 some
5 4 some
6 5 some
7 6 some
8 7 some
9 8 <NA>
10 9 <NA>
11 10 <NA>
This is expected (Note that the printout form for the the NA
value changes. Commenter Tensibai explains that: "NA
is the "numeric" NA, and <NA>
is the character one, as a vector (which a df column is) can only be of one type, when you enter a character value it gets coerced to character and that's why the NA representation change.")
though, another mystery).
However, using &&
, patch operation reduces to NA
:
mhbins <- data.frame(val=seq(0,10),bin=rep(c(NA),length.out=11))
val bin
1 0 NA
2 1 NA
3 2 NA
4 3 NA
5 4 NA
6 5 NA
7 6 NA
8 7 NA
9 8 NA
10 9 NA
11 10 NA
mhbins$bin[1 <= mhbins$val && mhbins$val <= 7] <- "some"
val bin
1 0 <NA>
2 1 <NA>
3 2 <NA>
4 3 <NA>
5 4 <NA>
6 5 <NA>
7 6 <NA>
8 7 <NA>
9 8 <NA>
10 9 <NA>
11 10 <NA>
I don't understand what's going on here.