& vs && in R

Copy from: https://stackoverflow.com/questions/6558921/boolean-operators-and

The shorter ones are vectorized, meaning they can return a vector, like this:

((-2:2) >= 0) & ((-2:2) <= 0)

[1] FALSE FALSE TRUE FALSE FALSE

The longer form evaluates left to right examining only the first element of each vector, so the above gives

((-2:2) >= 0) && ((-2:2) <= 0)

[1] FALSE

As the help page says, this makes the longer form "appropriate for programming control-flow and [is] typically preferred in if clauses."

你可能感兴趣的:(& vs && in R)