R reshape a vector into multiple columns

喜你入骨 提交于 2019-11-27 22:50:56

You can do

dim(d) <- c(10, 10)
d <- t(d)

or

d <- matrix(d, nrow = 10, byrow = TRUE)

If you want to convert a predifined list to a matrix (e.g. a 5*4 matrix), do

yourMatrix <- matrix(unlist(yourList), nrow = 5, ncol = 4)

It is worth noting that the matrix is created by columns, which means your data will be filled into the matrix by columns. So, if you want to the matrix created by rows, simply using t(), such as

yourMatrix <- matrix(unlist(yourList), nrow = 4, ncol = 5)  # exchanges the cols and rows
yourMatrix <- t(yourMatrix)  # matrix transpose
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!