R Programming By Example
上QQ阅读APP看书,第一时间看更新

Numerics

Numbers in R behave pretty much as you would mathematically expect them to. For example, the operation 2 / 3 performs real pision, which results in 0.6666667 in R. This natural numeric behavior is very convenient for data analysis, as you don't need to pay too much attention when using numbers of different types, which may require special handling in other languages. Also the mathematical priorities for operators applies, as well the use of parenthesis.

The following example shows how variables can be used within operations, and how operator priorities are handled. As you can see, you may mix the use of variables with values when performing operations:

x <- 2
y <- 3
z <- 4
(x * y + z) / 5
#> [1] 2

The modulo operation can be performed with the %% symbol, while integer pision can be performed with the %/% symbol:

7 %% 3
#> [1] 1 7 %/% 3
#> [1] 2