首页 > 解决方案 > 将元素附加到列表数组中的列表

问题描述

我有一个列表数组:

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

我怎样才能做到这一点?

标签: r

解决方案


你可以试试 :

M[[1]] <- list(M[[1]], x)

M[1]
#[[1]]
#[[1]][[1]]
#[1] 0 0

#[[1]][[2]]
#[1] 1 2

推荐阅读