R 将元素附加到列表数组中的列表
我有一系列的清单:R 将元素附加到列表数组中的列表,r,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 我试过了 > M[1][2] = x Warning messages: 1: In M[1][2] = x : number of items to replace is not a multiple of replacement
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
我试过了
> 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]
[[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
M[[1]]在设置名称后,我们还可以通过modifyList
动态执行此操作
names(M) <- seq_along(M)
modifyList(M, list(`1` = list(M[[1]], x)))
names(M)可能lappy(M,function(i)c(i,x))
?或者您想添加到第一个列表:M[[1]][1:2]我想有5个向量列表,所以我可以向这些列表添加向量。您注释的代码只添加向量每个列表的第一个元素,我需要添加一个向量作为列表的第二个、第三个等元素,这是我的问题。你能编辑你的帖子并显示你的预期输出吗?
M[[1]] <- list(M[[1]], x)
M[1]
#[[1]]
#[[1]][[1]]
#[1] 0 0
#[[1]][[2]]
#[1] 1 2
names(M) <- seq_along(M)
modifyList(M, list(`1` = list(M[[1]], x)))