1. Generate vector x of arithmetic sequence
x <- 1:10 # Assign x vector to 1 2 3 4 5 6 7 8 9 10
The result is
> x
[1] 1 2 3 4 5 6 7 8 9 10
2. Modify all the values of x to 0
x[] <- 0 #A very concise assignment method, it is recommended to usex[1:length(x)] <- 0 #Assignment method not recommended
The result is:
> x[] <- 0
> x
[1] 0 0 0 0 0 0 0 0 0 0
3. Use seq function
x <- seq(from = 1.5 , to = 8.2 ,by = 2.4)
The result is:
> x
[1] 1.5 3.9 6.3
4. Use the rep function
> w <- c("ab","123") > rep(w,2) [1] "ab" "123" "ab" "123" > rep(w,c(2,3)) [1] "ab" "ab" "123" "123" "123" >
5. Modify the vector dimensions
> x <- 1:8 > x [1] 1 2 3 4 5 6 7 8 > attr(x,"dim") <- c(2,4) > x [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 > dim(x) <- c(4,2) > x [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8
See online help for details.
Summarize
This is the end of this article about the simplest vector assignment method in R language. For more related R language vector assignment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!