r - 将元素附加到列表数组中的列表
问题描述
我有一个列表数组:
M <- array(list(numeric(2)), 5)
> M
[[1]]
[1] 0 0
[[2]]
[1] 0 0
[[3]]
[1] 0 0
[[4]]
[1] 0 0
[[5]]
[1] 0 0
我想将固定长度的向量附加到那些列表中,例如 2,就像这个
x = as.numeric(1:2)
我试过了
> M[1][2] = x
Warning messages:
1: In M[1][2] = x :
number of items to replace is not a multiple of replacement length
2: In M[1][2] = x :
number of items to replace is not a multiple of replacement length
> M[1]
[[1]]
[1] 0 0
和
> M[1][[2]] = x
Warning message:
In M[1][[2]] = x :
number of items to replace is not a multiple of replacement length
> M[1]
[[1]]
[1] 0 0
我希望输出是这样的:
> M[1]
[[1]]
[1] 0 0
[[2]]
[1] 1 2
我怎样才能做到这一点?
解决方案
你可以试试 :
M[[1]] <- list(M[[1]], x)
M[1]
#[[1]]
#[[1]][[1]]
#[1] 0 0
#[[1]][[2]]
#[1] 1 2
推荐阅读
- django - 创建新对象时如何更新外键上的字段?
- javascript - 更新用户生成标记的搜索结果
- php - 如何在现有 xml php 的末尾添加新节点
- python - 如何在 mpi4py 上的不同时间发送不同的东西
- apache-spark - 当 spark 动态分配为 true 时,EMR Cluster 显示过多的执行程序
- php - WordPress - 部分显示在菜单中的自定义类别
- .htaccess - 如果在 htaccess 中不同
- java - 如何将输入传递给 Kubernetes 容器内的 Java 应用程序?
- r - 根据 vcf 文件中的备用列重新排序变体效果
- javascript - Javascript:将另一个类的方法绑定到函数方法中的“this”关键字