Home > front end >  How to extract nested elements from a complex list?
How to extract nested elements from a complex list?

Time:06-23

I am working with a list with this structure. I want to extract the "contactId" of every contact into a new list.

surveys<-list(


  list(
    list(contactId = 2234, age= 24, unsuscribed = FALSE), 
    list(contactId = 6234, age= 23, unsuscribed = FALSE),
    list(contactId = 8234, age= 21, unsuscribed = FALSE)
    ),
  
  list(
    list(contactId = 1124, age= 28, unsuscribed = FALSE), 
    list(contactId = 1874, age= 15, unsuscribed = FALSE),
    list(contactId = 1674, age= 35, unsuscribed = FALSE),
    list(contactId = 1324, age= 45, unsuscribed = FALSE),
    list(contactId = 1234, age= 65, unsuscribed = FALSE)
  ),
  
  
  list(
    list(contactId = 1334, age= 18, unsuscribed = FALSE), 
    list(contactId = 1224, age= 45, unsuscribed = FALSE)
    

  )
) 

I am using the following line of code and it returns me all the data of the first contact of each sublist.

sapply(surveys, "[[",1)

Any help will be appreciated. Thanks in advance.

CodePudding user response:

The sapply returns a matrix with elements as list. We could extract further using the rownames

unlist(sapply(surveys, "[[",1)['contactId',])

If we want to extract all the elements, do a nested lapply/sapply and extract by the list names in the inner list

lapply(surveys, function(x) sapply(x, function(y) y[['contactId']]))

-output

[[1]]
[1] 2234 6234 8234

[[2]]
[1] 1124 1874 1674 1324 1234

[[3]]
[1] 1334 1224

Or another option is to use a recursive function (rrapply) to extract from the inner most vector

library(purrr)
library(rrapply)
library(magrittr)
rrapply(surveys,  classes = c("ANY", "vector"),
  f = function(x, .xname)  x[.xname == 'contactId']) %>%
  map(unlist, use.name = FALSE)
[[1]]
[1] 2234 6234 8234

[[2]]
[1] 1124 1874 1674 1324 1234

[[3]]
[1] 1334 1224

CodePudding user response:

You could also write a small function to do this:

get_elem <- function(x, elem){
  if(!is.list(x[[1]])) x[[elem]]
  else sapply(x, get_elem, elem)
}

get_elem(surveys, 'contactId')
[[1]]
[1] 2234 6234 8234

[[2]]
[1] 1124 1874 1674 1324 1234

[[3]]
[1] 1334 1224

 get_elem(surveys, 'age')
[[1]]
[1] 24 23 21

[[2]]
[1] 28 15 35 45 65

[[3]]
[1] 18 45
  • Related