显式地删除列表元素

12

我有这个列表:

myList <- list(rnorm(10), rnorm(10), rnorm(10))

names(myList) <- c("one", "two", "three")

$one
 [1] -0.07587506 -0.09997924 -0.41846732  1.41542651 -0.58678093  0.56909465 -1.11074541
 [8]  1.94663786  0.46381799 -0.11458166

$two
 [1]  0.98883679 -0.06305794 -0.78961229  1.21091484  0.19636700  0.27458057  0.12374154
 [8]  0.83782946 -0.79627870  0.97675486

$three
 [1]  0.67033455 -0.80243815  0.08716750 -2.90455146 -0.02433571 -0.93062428 -0.16886116
 [8] -0.60927976 -1.77758270 -1.05033148

我想从列表中删除 twothree,并且我想使用 "two" 和 "three" 来引用这些元素。我尝试过:

myList <- myList[[-c("two", "three")]]

...出现了错误。

如何将列表中的twothree移除,并用 "two" 和 "three" 来引用它们?

1个回答

26
myList[which(names(myList) %in% c("two","three"))] <- NULL

3
如果您尝试逐一删除元素,这将更容易理解: myList[2] <- NULL; myList$three <- NULL - MrGumble
17
或者只需使用myList[c("two", "three")] <- NULL就可以了。这将删除myList中名称为"two"和"three"的元素。 - flodel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接