-1

I have a list, one element of which is shown below

[[1]]

[[1]]$`id`

[1] "id"

[[1]]$num

[1] 100

To extract all the "num" elements from the list, I can use the following

nums=laply(x,`[[`,2)

Is there any way to replace the 2 in the laply expression with something like $num?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
anup
  • 33
  • 3
  • 1
    See [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ways to share data that can be copy/pasted into R. Be sure to give the desired output for your sample input. – MrFlick Apr 20 '16 at 17:33

1 Answers1

0

I think it should be sufficient to replace 2 with "num", like in the example below.

x <- rep(list(list(id = "id", num = 100)), 3)

nums <- lapply(x,`[[`,2)

nums2 <- lapply(x,`[[`, "num")

identical(nums, nums2)
[1] TRUE