重复函数是个好东西,省去了循环生成特定元素的功夫。
rep(x, times = 1, length.out = NA, each = 1)
# times : 控制x重复几次
# each : 是x中的元素每个生成几次
##Examples
> rep(1:3, 4)
[1] 1 2 3 1 2 3 1 2 3 1 2 3
> rep(1:3, each = 4)
[1] 1 1 1 1 2 2 2 2 3 3 3 3
> a<-seq(from = 1, to = 3, by = 1) #除了`:`外,还可以用seq函数生成等差数列
> rep(a, each = 4)
[1] 1 1 1 1 2 2 2 2 3 3 3 3
> rep(c("a", "b", "c"), each = 4)
[1] "a" "a" "a" "a" "b" "b" "b" "b" "c" "c" "c" "c"
完。
网友评论