carljong2019
carljong2019

Reputation: 107

R get elements from a list using another list of indices

I have a list of keywords. To access a keyword on this list I type keyword$Keyword[1]. I have another list, called abc, of indices. If I look at the list I see:

[[1]]
[1] 1303496

[[2]]
[1] 2345

which are indices from the elements in the keyword list.

How do I use the elements from the abc list to list out the keywords?

I'm thinking I would use which, but which is for a vector and these are lists.

Thanks

Upvotes: 0

Views: 575

Answers (1)

s__
s__

Reputation: 9485

If I've understood well, you got something like this:

keyword <- list('a','b','c','d')
index <- list(1,3)

Now let's think you need the keywords with index 1 and 3 in index:

keyword[unlist(index)]

[[1]]
[1] "a"

[[2]]
[1] "c"

Upvotes: 1

Related Questions