In R language, vectors of different lengths can also be added and multiplied, and the rules of multiplication are similar to addition
1, add vectors of the same length
> x<- 1:4 > y<- 1:4 > z<- x+y > z
[1] 2 4 6 8
The rules are x[1]+y[1], x[2]+y[2], x[3]+y[3], x[4]+y[4]
> x<- 1:4 > y<- 1:4 > z<- x*y > z [1] 1 4 9 16
Multiplication is similar
2. Add up vectors of different lengths
> x<- 1:4 > y<- 1:3 > z<-x+y Warning message: In x + y : The length of a long object is not an integer multiple of a short object length > z [1] 2 4 6 5 >
Note that R returns a warning message instead of an error message, so this operation is actually executed.
The rules of this type are x[1]+y[1], x[2]+y[2], x[3]+y[3], x[4]+y[1] (because y[3] ends and enters another loop)
Multiplication rules are similar
> x<- 1:4 > y<- 1:3 > z<- x*y Warning message: In x * y : The length of a long object is not an integer multiple of a short object length > z [1] 1 4 9 4
In addition, the resulting vector length is the length of the longest vector
> x<- 1:4 > y<- 1:3 > z<- 2:3 > w<- x+y+z Warning message: In x + y : The length of a long object is not an integer multiple of a short object length > w [1] 4 7 8 8 > v<-x*y*z Warning message: In x * y : The length of a long object is not an integer multiple of a short object length > v [1] 2 12 18 12 >
But there is a problem here,
> x<- 1:4 > y<- 1:3 > z<- 2:3 > x+y+z [1] 4 7 8 8 Warning message: In x + y : The length of a long object is not an integer multiple of a short object length > x+z+y [1] 4 7 8 8 Warning message: In x + z + y : The length of a long object is not an integer multiple of a short object length > z+x+y [1] 4 7 8 8 Warning message: In z + x + y : The length of a long object is not an integer multiple of a short object length ><span style="color:#ff0000;"> z+y+x [1] 4 7 8 7</span> Warning message: 1: In z + y : The length of a long object is not an integer multiple of a short object length 2: In z + y + x : The length of a long object is not an integer multiple of a short object length > z*x*y [1] 2 12 18 12 Warning message: In z * x * y : The length of a long object is not an integer multiple of a short object length > z*y*x [1] 2 12 18 8 Warning message: 1: In z * y : The length of a long object is not an integer multiple of a short object length 2: In z * y * x : The length of a long object is not an integer multiple of a short object length >
I don't know if you have noticed it, is our method wrong?
First, addition and multiplication operations are calculated from left to right in sequence without other priority levels such as brackets.
Let's take a look
> x<- c(1,2,3,4) > y<- c(1,2,3) > z<- c(2,3) > x+y [1] 2 4 6 5 > x+y+z [1] 4 7 8 8
> z+y [1] 3 5 5 > z+y+x [1] 4 7 8 7
Therefore, the order of adding vectors of different lengths is also very important.
Supplement: R language vector_common vector operations
Vector operations and logical operations
> 2+3 [1] 5 > "+"(2,3) [1] 5 > x<-c(1,2,4) > x+c(5,0,-1) [1] 6 2 3
These are relatively simple, they are simple scalar operations and vector operations. They are just operators that can be placed in front and the corresponding elements of the vector need to be added.
> x<-c(1,2,4) > x*c(5,0,-1) [1] 5 0 -4 > x<-c(1,2,4) > x/c(5,4,-1) [1] 0.2 0.5 -4.0 > x%%c(5,4,-1) [1] 1 2 0
The following points should be noted for the operation of these steps: * operation is to multiply the corresponding elements of the vector, which is different from the multiplication of the matrix in linear algebra. / operation is to divide the corresponding elements. The %% operation is to divide the corresponding element into the remainder.
Vector index
> y<-c(1.2,3.9,0.4,0.12) > y[c(1,3)] [1] 1.2 0.4 > y[2:3] [1] 3.9 0.4 > v<-3:4 > y[v] [1] 0.40 0.12
These are relatively easy, you can see them at first sight, without explaining them in detail
> x<-c(4,2,17,5) > y<-x[c(1,1,3)] > y [1] 4 4 17
This example is to say that element repetition is allowed
> z<-c(5,12,13) > z[-1] [1] 12 13 > z[-1:-2] [1] 13
A negative subscript means we want to remove the corresponding element.
Create vectors with: operator
> 5:8 [1] 5 6 7 8 > 5:1 [1] 5 4 3 2 1 > i<-2 > 1:i-1 [1] 0 1 > 1:(i-1) [1] 1
: The operator is actually to get a series of arithmetic sequences, which is relatively simple, but we need to talk about specifically about 1:i-1 and 1:(i-1). In fact, there is a problem of operator priority. 1:i-1 is to calculate 1:i to get 1 2, then subtract 1 to get 0 1, while 1:(i-1) is to calculate i-1 first to get 1 and then calculate 1:1. The final answer is 1.
Create vectors with seq()
This function is also used to generate arithmetic sequences. See examples for the specific usage
> seq(from=12,to=30,by=3) [1] 12 15 18 21 24 27 30
This code indicates that an arithmetic sequence is generated from 12 to 30, with a tolerance of 3.
> seq(from=1.1,to=2,length=10) [1] 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0
This represents arithmetic sequence of 10 numbers generated from 1.1 to 2
Repeat vector constants using rep()
The format of the call is rep(x,times), which represents the creation of times*length(x) elements of the vector. This vector consists of x repeated times.
> x<-rep(8,4) > x [1] 8 8 8 8 > rep(c(5,12,13),3) [1] 5 12 13 5 12 13 5 12 13 > rep(1:3,2) [1] 1 2 3 1 2 3 > rep(c(5,12,13),each=2) [1] 5 5 12 12 13 13
The last each represents the number of times each element is repeated in the vector. If each element is repeated, it is no longer the entire vector repeat.
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.