假设我有一个R中的向量如下:
d<-seq(1,100)
我想将这个向量重塑为一个10x10的矩阵,以便我可以得到以下数据:
[,1] [,2] [,3] .. [,10]
1 2 3 .. 10
11 12 13 .. 20
21 22 23 .. 30
..
91 92 93 .. 100
我尝试使用reshape函数,但它没有起作用。请问有人可以帮忙吗?
dim(d) <- c(10, 10)
d <- t(d)
或者
d <- matrix(d, nrow = 10, byrow = TRUE)
yourMatrix <- matrix(unlist(yourList), nrow = 5, ncol = 4)
值得注意的是,矩阵是按列创建的,这意味着您的数据将按列填充到矩阵中。因此,如果您想按行创建矩阵,只需使用 t(),例如:
yourMatrix <- matrix(unlist(yourList), nrow = 4, ncol = 5) # exchanges the cols and rows
yourMatrix <- t(yourMatrix) # matrix transpose