Search code examples
rlistsubsetnested-listssublist

List only values from a subset of a subset of list in R


Hi my datastructure is attached.

this

I need to access just the mean under each [[ element under extra. I can do this manually individually opt.state[["opt.path"]][["env"]][["extra"]][[1]][["mean"]] which gives me NA but there's 100 elements like this.

I followed the solution from this problem of : subset of a subset of a list

using this lst <- lapply(opt.state[["opt.path"]][["env"]][["extra"]], function(x) x[["mean"]]) but end up getting two extra columns I don't need :

enter image description here

How do I go about getting just 1-column list with the values?

Cheers


Solution

  • You can unlist(lst) then you will get a vector of 100 mean values. Columns Name and Type would appear for every list object in R when you View them, Or you can use sapply() function which would directly return a vector in this case, instead of a list.