在R编程中复制向量的元素——rep()方法
在 R 编程语言中,通过以指定次数重复给定数字向量来创建向量的一个非常有用的函数是 rep()。
The general structure of rep() : rep(v1,n1).
Here, v1 is repeated n1 times.
R – 复制向量的元素
rep() 函数的形式:
- 代表(v1,次=)
- 代表(v1,每个=)
- 代表(v1,长度=)
示例 1:
R
# Replicate '0' 5 time
rep(0, 5)
R
# 1,2,3 repeated 3 times in sequencially
rep(1:3,times=3)
R
# 1,2,3 repeated 3 times
rep(1:3,each=3)
R
# generate a vector 1,2,3
x<-1:3
# vector x is replicated such that the
# length is five.
rep(x, length=5)
R
# 1 is replicated 2 times, and so on
rep(x,c(2,1,3))
输出:
[1] 0 0 0 0 0
示例 2:
代表(v1,次=)
R
# 1,2,3 repeated 3 times in sequencially
rep(1:3,times=3)
输出:
[1] 1 2 3 1 2 3 1 2 3
示例 3:
代表(v1,每个=)
R
# 1,2,3 repeated 3 times
rep(1:3,each=3)
输出:
[1] 1 2 3 1 2 3 1 2 3
示例 4:
代表(v1,长度=)
R
# generate a vector 1,2,3
x<-1:3
# vector x is replicated such that the
# length is five.
rep(x, length=5)
输出:
1 2 3 1 2
示例 5:
R
# 1 is replicated 2 times, and so on
rep(x,c(2,1,3))
输出:
1 1 2 3 3 3