1 Vector
1.1 The loop of the vector is padded
The length of the two vectors is not the same, and for the operation, the short vectors are repeated, for example
>a<-c (a)
>b<-c (2,3,4,5)
>a+b
[1] 3 5 7 6
Warning message:
In A+b:longer object length was not amultiple of shorter object length
>a/b
[1] 6.0000000 0.6666667 0.7500000 2.4000000
Warning message:
In A/b: longer object length is not amultiple of shorter object length
This is not the same as the usual other language habits. 1.2 vector Generation
In addition to C () in the R language, there are some useful methods: 1.2.1 seq (): Produces a regular sequence, interval can be set
> seq (10,20,4)
[1] 10 14 18
Starting from 10, the pitch is 4 until 20
> seq (0, by = 0.03, length = 15)
[1]0.00 0.03 0.06 0.09 0.12 0.15 0.18 0.21 0.24 0.27 0.30 0.33 0.36 0.39 0.42
Starting from 0, a vector of length 15 with a spacing of 0.03 is generated 1.2.2 Rep (): produces a regular sequence of variables, repeats several times
> Rep (1:3, 1:3)
[1] 1 2 2 3 3 3
1 repeats 1 times, 2 repeats 2 times, 3 repeats 3 times
> Rep (1:3, Rep (2, 3))
[1] 1 1 2 2 3 3
1 to 3 repeats 3 times 1.3 Some vector function vectors
SUM () #和
Max () #最大值
Min () #最小值
Range () #取值范围
Mean () #平均值
var () #方差
Sort () #从小到大排序
Rev () #反排列, so the order from large to small should be Rev (sort (XX))
Prod () #乘积, so factorial is prod (1:n)
Append () #追加函数
Replace () #t替换
Any () #any (x> 8) vector x has a value greater than 8
All () #all (x>8) vector x whether any of the elements are greater than 8
Union () #求并集
Intersect () #交集
Setdiff () #补集
Setequal () #测试集合是否相等
1.3 Index of the vector
The index of vectors in the R language is somewhat like an array subscript.
> y<-1:100
> Y[seq (1,to=100,by =6)]
[1] 1 7 13 19 25 31 37 43 49 55 61 6773 79 85 91 97
Seq (1,to =100,by = 6), is a vector and then takes this vector as the index to access Y.
Negative subscript means removal of the corresponding element
> Y[-90:-1]
[1] 91 92 93 94 95 96 97 98 99 100
> Y[2:10]-y[3:11]
[1]-1-1-1-1-1-1-1-1-1
Filtering with an index
> z*z<9
[1] True True false false
> Y[z*z<9]
[1] 1 2 6 7 1112 16 17 21 22 26 27 31 32 36 37 41 42 46 47 51 52 56 57 61 62 66 67 71 72 7677 81 82 86 87 91 92 96 97