【发布时间】:2020-06-09 23:27:08
【问题描述】:
我想在 R 中添加一个列表作为另一个列表的元素。这似乎可行,但我不知道语法。我尝试了多种不同的方法,并提出了以下四个示例来说明问题。
我的目标是接近示例 3,我可以在其中编写类似于:a$b[5]$c = 7 的语句。
示例 #1
> q = list()
> q[1] = list(1,2)
Warning message:
In q[1] = list(1, 2) :
number of items to replace is not a multiple of replacement length
示例 #2:
> a = list()
> a$b = list()
> a$b[1] = list()
> a$b[2] = list(1)
> a$b[3] = list(1,2)
Warning message:
In a$b[3] = list(1, 2) :
number of items to replace is not a multiple of replacement length
示例 #3:
> a$b[5]$c = 7
Warning message:
In a$b[5]$c = 7 :
number of items to replace is not a multiple of replacement length
示例 #4:
第 1 部分
> w = list("1"=list(1,1), "2"=list(2,2))
> w
$`1`
$`1`[[1]]
[1] 1
$`1`[[2]]
[1] 1
$`2`
$`2`[[1]]
[1] 2
$`2`[[2]]
[1] 2
第 #2 部分
> w[1] = 5
> w
$`1`
[1] 5
$`2`
$`2`[[1]]
[1] 2
$`2`[[2]]
[1] 2
【问题讨论】:
-
使用双括号:
q[[1]] = list(1, 2)