1. Assignment
推荐使用 variable <- 666
这种赋值方法
2. Calculation
符号 | 运算 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除(正常除) |
^ | 乘方 |
log() | log computes logarithms, by default natural logarithms, log10 computes common (i.e., base 10) logarithms, and log2 computes binary (i.e., base 2) logarithms. The general form log(x, base) computes logarithms with base base. |
exp(x) | e 的x次方 |
%% | 取余数 |
%/% | 取整(x%/%y 即求x/y的整数部分) |
NB:和python的区别:
(1)python和R中乘都是用*
表示。但是python中乘方是**
,而R中是^
。
(2)python 3和R中/
都可以表示除法,但python中//
表示地板除而R不可以。
(3)python中exp() pi log()
等都需要调用math包,然而R不要
(4)python中%
表示取余数,而R中%%
表示取余数%/%
表示取整数。
Example:
3. rounding
函数 | 简述 | 详述 from R |
---|---|---|
floor(x) | 向下取整 | floor takes a single numeric argument x and returns a numeric vector containing the largest integers not greater than the corresponding elements of x. |
ceiling(x) | 向上取整 | ceiling takes a single numeric argument x and returns a numeric vector containing the smallest integers not less than the corresponding elements of x. |
round(x, digits = 0) | 四舍五入取整 | round rounds the values in its first argument to the specified number of decimal places (default 0). See ‘Details’ about “round to even” when rounding off a 5. |
trunc(x, …) | 向0取整 | trunc takes a single numeric argument x and returns a numeric vector containing the integers formed by truncating the values in x toward 0. |
signif(x, digits=6) | 保留给定位数的精度 | signif rounds the values in its first argument to the specified number of significant digits. |
Arguments:
Arguments | description (from R) |
---|---|
x | a numeric vector. Or, for round and signif, a complex vector. |
digits | integer indicating the number of decimal places (round) or significant digits (signif) to be used. Negative values are allowed (see ‘Details’). |
… | arguments to be passed to methods. |
Example:
4. Operating vectors
a. Generating
b. indexing
>vec4 <- seq(from = 1, to = 12, by = 1.333)
>vec4
[1] 1.000 2.333 3.666 4.999 6.332 7.665 8.998 10.331 11.664
NB: (1)区别于python和其他语言,R的是从1开始索引
(2)在R中index的负号表示出去该index。比如vec4[-4],表示vec4中除去第四个元素的其他所有元素。而python中-
号表示倒数,vec4[-4]会表示倒数第四个元素(python倒着索引从-1开始)。
c. comparison
5. Operating list
vec1 <- c(1, 2, 3)
vec2 <- 1:11
vec3 <- rep(x = 4, 7)