我是 R 新手。我想使用 cbind
使用 for
循环的结果填充一个空矩阵。我的问题是,如何消除矩阵第一列中的 NA。我在下面添加了我的代码:
output<-matrix(,15,) ##generate an empty matrix with 15 rows, the first column already filled with NAs, is there any way to leave the first column empty?
for(`enter code here`){
normF<-`enter code here`
output<-cbind(output,normF)
}
输出是我期望的矩阵。唯一的问题是它的第一列充满了 NA。如何删除这些 NA?
最佳答案
矩阵
的默认值是有 1 列。要显式拥有 0 列,您需要编写
matrix(, nrow = 15, ncol = 0)
更好的方法是预先分配整个矩阵,然后填充它
mat <- matrix(, nrow = 15, ncol = n.columns)
for(column in 1:n.columns){
mat[, column] <- vector
}
关于r - 如何在R中创建一个空矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21585721/