我有一个向量v
,我想创建以下矩阵。我怎么能在R?中做到这一点?
v = c(1, 2, 3, 4)
> m = matrix(c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,4), nrow=4)
> m
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
[4,] 1 2 3 4
答案 0 :(得分:3)
请参阅?matrix
和nrow
,ncol
,byrow
个参数:
matrix(v, nrow=4, ncol=4, byrow=TRUE)
# [,1] [,2] [,3] [,4]
#[1,] 1 2 3 4
#[2,] 1 2 3 4
#[3,] 1 2 3 4
#[4,] 1 2 3 4