Reputation: 121
I have a dataframe kegglist
, the pertinent section of which I show below:
> kegglist[131:145,]
V1 V2
131 A0AVT1 hsa04120
132 A0PJZ3 hsa00514
133 A1A4S6 hsa05100
134 A1A4Y4 hsa05145
135 A1L167 hsa04120
136 A2RTX5 hsa00970
137 A3KFT3 hsa04740
138 A4D0S4 hsa05146
139 A4D0S4 hsa04512
140 A4D0S4 hsa04510
141 A4D0S4 hsa05200
142 A4D0S4 hsa05222
143 A4D0S4 hsa05145
144 A4D2G3 hsa04740
145 A5D8V6 hsa04144
I also have a vector listx
which contains some, but not all, of the IDs in V1
of kegglist
:
> listx
[1] A1L167 A2RTX5 A3KFT3 A4D0S4
What I want to do is subset kegglist$V2
based on the IDs in kegglist$V1
that are present in listx
. So, in this example, the result should look like this:
> result
[1] hsa04120 hsa00970 hsa04740 hsa05146 hsa04512 hsa04510 hsa05200 hsa05222 hsa05145
Please note that in the real data, not all IDs in listx
are found consecutively in kegglist$V1
. Thank you for the help.
I tried
result <- kegglist$V1[kegglist$V2 %in% listx]
to no avail.
Upvotes: 0
Views: 48
Reputation: 1147
Try:
kegglist$V2[kegglist$V1 %in% listx]
Or:
kegglist$V2[which(kegglist$V1 %in% listx)]
Upvotes: 2