Operators
R's operators look familiar from other languages, with a couple of R-specific twists worth knowing before you hit them by surprise.
Arithmetic
R arithmetic.R
7 %% 3 7 %/% 3 2^5
Console
[1] 1 [1] 2 [1] 32
%% is modulo (remainder), %/% is integer division, and ^ raises to a power — all separate from the plain /, which always does regular floating-point division.
Comparison
R comparison.R
5 > 3 5 == 5 "abc" == "abc"
Console
[1] TRUE [1] TRUE [1] TRUE
Logical: && / || vs & / |
R logical.R
TRUE && FALSE TRUE || FALSE c(TRUE, FALSE) & c(TRUE, TRUE)
Console
[1] FALSE [1] TRUE [1] TRUE FALSE
Note:
&& and || look at a single logical value each and are what you use inside an if() condition. & and | are vectorized — they compare two vectors element by element and return a vector of results, like the third example above. Passing a vector longer than 1 to &&/|| is an error in current R versions (it used to just warn and quietly use the first element), so picking the right one matters more than it looks like it should.