简体   繁体   中英

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.

Here is the issue I'm trying to address: I have a list of vectors (GO_list) and another vector (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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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