Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在常见情况下重写向量创建代码?_R_Vector - Fatal编程技术网

R 如何在常见情况下重写向量创建代码?

R 如何在常见情况下重写向量创建代码?,r,vector,R,Vector,我需要创建向量cx=(1,1,1,2,2,2,3,3)。我写了一个代码 k <- 3 tmp <- matrix(0, k, k) tmp[1,] <-rep(c(1), times = k) tmp[2,] <-rep(c(2), times = k) tmp[3,] <-rep(c(3), times = k) cx <-c(t(tmp)) cx #[1] 1 1 1 2 2 2 3 3 3 rep功能足够好,但Kronecker产品值得一提 kron

我需要创建向量
cx=(1,1,1,2,2,2,3,3)
。我写了一个代码

k <- 3
tmp <- matrix(0, k, k)
tmp[1,] <-rep(c(1), times = k)
tmp[2,] <-rep(c(2), times = k)
tmp[3,] <-rep(c(3), times = k)

cx <-c(t(tmp))
cx
#[1] 1 1 1 2 2 2 3 3 3

rep
功能足够好,但Kronecker产品值得一提

kronecker(1:3, rep(1, 3))

使用
rep
each
参数:
rep(1:3,each=3)
。。。
kronecker(1:3, rep(1, 3))