r - 如何在R中创建一个空矩阵?

标签 r matrix na

我是 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/

相关文章:

r - 得到线性 SVM 回归线的方程

python - 通过添加到末尾来扩展矩阵中的每一行?

matrix - Cuda/PyCuda - 大型矩阵遍历和 block /网格大小

Python - 二维列表的里程表样式组合

r - 如何将特定长度和数量的随机连续间隙插入向量中?

r - R 中 NA 的累积返回

r - R如何处理NA值并通过回归删除值

r - 在 ggplot 中,在 LOESS 中将 y 限制为 >0

r - 绘制改变母样形状的样例

r - 如何向填充等高线图添加点?