shayelk shayelk - 5 months ago 27
R Question

R - extracting a list element by name when the name is in a variable

I'm trying to extract a list element by name, when the name is stored in a variable. i.e.:

myList <- list(a = 1, b = 2, c = 3) ## list definition
## I can extract the second element like this:
## but say I have a variable:
to_extract <- "b"
##can I do something like this?


ycw ycw
Answer Source

The following should all work.


`[[`(myList, to_extract)

pluck(myList, to_extract)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download