list 显式删除列表的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16778161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 02:00:46 来源:igfitidea点击:
Remove elements of a list explicitly
提问by luciano
I have this list:
我有这个清单:
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
I want to remove two
and three
from the list and I want to refer to these elements using "two" and "three". I've tried:
我想从列表中删除two
和three
我想使用“二”和“三”来引用这些元素。我试过了:
myList <- myList[[-c("two", "three")]]
...which gives an error.
...这给出了一个错误。
How can I remove two
and three
from the list and refer them using "two" and "three"?
如何从列表中删除two
和three
并使用“二”和“三”来引用它们?
回答by Noam Ross
myList[which(names(myList) %in% c("two","three"))] <- NULL