【问题标题】:Is there a trick to looping with cbind?使用 cbind 循环有技巧吗?
【发布时间】:2014-10-24 08:55:01
【问题描述】:

我有 87 个长度为 4096 的向量。例如,

> head(d[[1]]$x)
[1] 1.676094 1.676323 1.676551 1.676780 1.677008 1.677237

我想将这些向量连接成一个矩阵,每个向量占据一列。因为单独绑定它们可以正常工作

Ds <- cbind(d[[1]]$x,d[[2]]$x,d[[3]]$x)

我认为这也可以工作

matrix() -> Ds
for(i in 1:87){
    cbind(d[[i]]$x) -> Ds[[i]]
}

但我得到了错误

Error in Ds[[i]] <- cbind(d[[i]]$x) : 
  more elements supplied than there are to replace

是否有关于 cbind 的特定内容不允许循环或我遗漏了什么?任何建议表示赞赏。

谢谢。

【问题讨论】:

    标签: r loops matrix vector cbind


    【解决方案1】:

    cbind 需要两个或更多参数。你可以试试:

    Ds<-c() 
    for(i in 1:87){
         Ds<-cbind(Ds,d[[i]]$x) 
    }
    

    【讨论】:

    • 有效。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2020-02-10
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多