我的数据位于 1 列 x 40000 行数据框中。我想让它成为一个 200 列 x 200 行的数据框。当我使用所有数值时,我使用下面的代码将我的向量转换为矩阵。
spematrix=data.matrix(spe)
matrix = matrix(spematrix, nrow = 200, ncol=200)
我现在不能使用它,因为我的数据包含非数字值(用逗号分隔的数字)。
我是 R 的新手,希望得到任何帮助。
最佳答案
听起来您已经知道该怎么做了。您可以使用 gsub
清理数据以删除 ,
字符:
spe <- gsub(",","",spe[,1]) # returns a character vector
spe <- as.numeric(spe) # convert to numeric
然后,
library(Matrix)
spematrix <- data.matrix(spe)
matrix = Matrix(spematrix, nrow = 200, ncol=200)
关于r - R中的一维数据帧到二维格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358051/