简体   繁体   English

在R中,如何基于另一个列表限制列表的列表?

[英]In R, how do I restrict a list of lists based on another list?

I feel like this must be simpler than I'm making it, but for some reason I've having some real difficulties even figuring out how to ask this question-- please feel free to suggest some better terminology here, because I think I'm even confusing myself. 我觉得这一定比我做的要简单,但是出于某种原因,我甚至在弄清楚如何提出这个问题上也遇到了一些实际困难,请随时在这里提出一些更好的术语,因为我认为我我什至困惑自己。 I'm very new to R and I think I am getting confused because I keep trying to accomplish this how I would do it with Python. 我对R还是很陌生,我觉得很困惑,因为我一直在努力做到这一点,就像使用Python一样。

Here is the issue I'm trying to address: I have a list of vectors (GO_list) and another vector (Targets). 这是我要解决的问题:我有一个向量列表(GO_list)和另一个向量(Targets)。 I simply want to create a new list of vectors (NewList) that only contains lists from GO_list if they match a term in my other list (targets). 我只想创建一个新的向量列表(NewList),该列表仅包含来自GO_list的列表(如果它们与我的其他列表(目标)中的术语匹配)。

For example: 例如:

>GO_list[1:5]

$ENSCPOP00000019422
[1] "GO:0006821" "GO:0055085" "GO:0006897"
$ENSRNOP00000017654
[1] "GO:0000165" "GO:0007169" "GO:0007399"
$ENSMUSP00000000365
[1] "GO:0006351" "GO:0006355" "GO:0006974" "GO:0007049" "GO:0008150" "GO:0040008"
 $ENSCPOP00000019426
[1] "GO:0006470" "GO:0016311"
$ENSCPOP00000019424
[1] "GO:0006886"

>Targets[1:5]
[1] "ENSMUSP00000104347" "ENSMUSP00000081003" "ENSMUSP00000134911" 
[3] "ENSMUSP00000081001" "ENSMUSP00000081002"

And so I only want to include GO_list[1] in NewList if ENSCPOP00000019422 is in Targets. 因此,如果ENSCPOP00000019422在Targets中,我只想在NewList中包含GO_list [1]。

Does this question make sense? 这个问题有意义吗?

Thanks for your time! 谢谢你的时间!

Something like this? 像这样吗

> x <- list(A=1, B=2, C=3, D=4, E=5)
> target<-list("C","D")
> x[names(x)%in%target]
$C
[1] 3

$D
[1] 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM