yasel
yasel

Reputation: 453

Subset nested list

I have a nested list and would like to filter for multiple conditions. I am aware that similar questions have been asked but for some reason the approaches there won't work on my list,..

myList <- list(list(list(FileName = list("05_C13_1.mzML"), Molecule = "Adenine", 
            Adduct = list("2M+H"), cons.Area = list(42158.2196614537))), 
            list(list(FileName = list("05_C13_2.mzML"), Molecule = "Phenylalanine", 
            Adduct = list("2M+H"), cons.Area = list(36879.9850931971))), 
            list(list(FileName = list("10_C13_2.mzML", "10_C13_2.mzML"), 
            Molecule = "Adenine", Adduct = list("M+K", "M+K"), cons.Area = list(
            512368.044002373, 60847.2653549584))))

That's the function I tried:

get_sublist <- function(lst, group_name) {
                lst[lapply(lst, function(x) x[[1]][[1]]) == group_name]
}

It works very well on the following list but for reasons I don't understand not on mine (also if i replace x[[1]][[1]] with x[[1]]),..

ThisListWorks <- list(list(list(group = "a", def = "control")), list(list(group = "b", 
        def = "disease1")))

The desired output for my example would be e.g.:

SubList1 <- get_sublist(myList, "Adenine")

SubList1
list(list(list(FileName = list("05_C13_1.mzML"), Molecule = "Adenine", 
    Adduct = list("2M+H"), cons.Area = list(42158.2196614537))), 
    list(list(FileName = list("10_C13_2.mzML", "10_C13_2.mzML"), 
    Molecule = "Adenine", Adduct = list("M+K", "M+K"), cons.Area = list(
    512368.044002373, 60847.2653549584))))

or:

SubList2 <- get_sublist(myList, "10_C13_2.mzML")

SubList2
list(list(list(FileName = list("10_C13_2.mzML", "10_C13_2.mzML"), 
    Molecule = "Adenine", Adduct = list("M+K", "M+K"), cons.Area = list(
    512368.044002373, 60847.2653549584))))

Upvotes: 0

Views: 45

Answers (1)

Douglas Mesquita
Douglas Mesquita

Reputation: 1021

I think the index that you are using (x[[1]][[1]]) is wrong. It is going to look for Adenine in the FileName entrie.

You can change the function to be more robust:

get_sublist <- function(lst, var, group_name) {
  lst[lapply(lst, function(x) x[[1]][[var]]) == group_name]
}

Then:

xx <- get_sublist(myList, var = "Molecule", group_name = "Adenine")
dput(xx)
list(list(list(FileName = list("05_C13_1.mzML"), Molecule = "Adenine", 
    Adduct = list("2M+H"), cons.Area = list(42158.2196614537))), 
    list(list(FileName = list("10_C13_2.mzML", "10_C13_2.mzML"), 
        Molecule = "Adenine", Adduct = list("M+K", "M+K"), cons.Area = list(
            512368.044002373, 60847.2653549584))))

It will work whenever the var level is not a list. For your second example, you have an extra level and then the above approach will not work.

I think your first level is not useful in this problem, so I dropped it and created a recursive function to handle any number of levels:

get_sublist <- function(lst, var, group_name) {

  if(!(var %in% names(lst))){
    pos <- sapply(X = lst, FUN = get_sublist, var = var, group_name = group_name)
  } else{
    if(is.list(lst[[var]])){
      values <- unlist(lst[[var]])
    } else{
      values <- lst[[var]]
    }

    if(group_name %in% values){
      return(TRUE)
    } else{
      return(FALSE)
    }
  } 

  lst[pos]
}

Then:

xx <- get_sublist(unlist(myList, recursive = F), var = "Molecule", group_name = "Adenine")
dput(xx)
list(list(FileName = list("05_C13_1.mzML"), Molecule = "Adenine", 
    Adduct = list("2M+H"), cons.Area = list(42158.2196614537)), 
    list(FileName = list("10_C13_2.mzML", "10_C13_2.mzML"), Molecule = "Adenine", 
        Adduct = list("M+K", "M+K"), cons.Area = list(512368.044002373, 
            60847.2653549584)))

and,

yy <- get_sublist(unlist(myList, recursive = F), var = "FileName", group_name = "10_C13_2.mzML")
dput(yy)
list(list(FileName = list("10_C13_2.mzML", "10_C13_2.mzML"), 
    Molecule = "Adenine", Adduct = list("M+K", "M+K"), cons.Area = list(
        512368.044002373, 60847.2653549584)))

Upvotes: 1

Related Questions