子集嵌套列表

时间:2019-02-05 19:27:57

标签: r list filter nested subset

我有一个嵌套列表,想过滤多个条件。我知道有人问过类似的问题,但由于某种原因,那里的方法在我的清单上不起作用,..

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))))

那是我尝试过的功能:

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

它在下面的列表中很好地工作,但是由于某些原因,我不明白我的原因(同样,如果我将x[[1]][[1]]替换为x[[1]])。.

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

我的示例所需的输出例如:

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))))

或:

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))))

1 个答案:

答案 0 :(得分:1)

我认为您正在使用的索引(x[[1]][[1]])是错误的。它将在Adenine实体中寻找FileName

您可以将功能更改为更强大:

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

然后:

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))))

只要var级别不是list,它将起作用。对于第二个示例,您有一个附加级别,那么上述方法将不起作用。

我认为您的第一个级别在此问题中没有用,所以我删除了它,并创建了一个递归函数来处理任意多个级别:

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]
}

然后:

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)))

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)))