1

I have a nested list like this one:

nested_list = list(children = list(CV = "hello", 
                                   children = list(CV = "out", 
                                                   children = list(CV = "there", 
                                                                   TE = "blaa")), 
                                   TE = "hello",
                                   children = list(CV = "tom", 
                                                   TE = "lisa", 
                                                   children = list(TE = "bob"))), 
                   children = list(CV = "sam", 
                                   children = list(CV = "out", 
                                                   children = list(CV = "there", 
                                                                   TE = "blaa", 
                                                                   other = "other element"))))

and I need to extract all elements that are named with "CV" or "TE". There might also be elements with other names as CV or TE.

The elements with names "TE" or "CV" can be lists themselves, so I need to extract the lists. Here´s some input with lists as elements that need to be extracted.

nested_list2 = list(children = list(CV = "hello", 
                                   children = list(CV = list(subject = "sub", value = list(key = "1", var = "45")), 
                                                   children = list(CV = "there", 
                                                                   TE = list(subject = "sub2")))))

The problem is the unknown depth level of nested lists. so I need a function that I can use on any nested list.

This is the expected output for nested_list:

exp_output = list(CV = "hello", 
                  CV = "out", 
                  CV = "there", 
                  TE = "blaa", 
                  TE = "hello", 
                  CV = "tom", 
                  TE = "lisa", 
                  TE = "bob", 
                  CV = "sam",
                  CV = "out", 
                  CV = "there", 
                  TE = "blaa")

and this would be the expected output of nested_list2:

exp_output2 = list(CV = list("hello"),
                   CV = list(subject = "sub", value = list(key = "1", var = "45")),
                   CV = list("there"),
                   TE = list(subject = "sub2"))

I know that the list elements will not have unique names, but I need this structure. Is there such a function? I haven´t found anything.

Any help appreciated.

3
  • Can you please share your expected output? Commented Oct 1, 2018 at 8:26
  • Question is unclear, did you mean something like unlist(nested_list)? Commented Oct 1, 2018 at 8:26
  • Sorry, I updated my question and added the expected output. There can be other named elements. Commented Oct 1, 2018 at 8:37

1 Answer 1

2

You can use a recursive function that appends a list :

res <- list()
fun <- function(x, nms){
  if(any(names(x) %in% nms)){
    res <<- c(res,x[names(x) %in% nms])
    x <- x[!names(x) %in% nms]
  }
  if (is.list(x)) lapply(x,fun,nms)
}
fun(nested_list2, c("CV","TE"))

res <- list()
fun(nested_list, c("CV","TE"))
str(res)
# List of 12
# $ CV: chr "hello"
# $ TE: chr "hello"
# $ CV: chr "out"
# $ CV: chr "there"
# $ TE: chr "blaa"
# $ CV: chr "tom"
# $ TE: chr "lisa"
# $ TE: chr "bob"
# $ CV: chr "sam"
# $ CV: chr "out"
# $ CV: chr "there"
# $ TE: chr "blaa"

res <- list()
fun(nested_list2, c("CV","TE"))
str(res)
# List of 4
# $ CV: chr "hello"
# $ CV:List of 2
# ..$ subject: chr "sub"
# ..$ value  :List of 2
# .. ..$ key: chr "1"
# .. ..$ var: chr "45"
# $ CV: chr "there"
# $ TE:List of 1
# ..$ subject: chr "sub2"
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Moody, thank you for this solution, it's helped me out. I was wondering if the function could be modified to be more self-contained by not using the global assign <<-?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.