【问题标题】:subsetting a nested list based on a conidtion in R根据 R 中的条件对嵌套列表进行子集化
【发布时间】:2021-08-01 20:51:26
【问题描述】:

我的嵌套列表如下所示:

  myList <- list(structure(list(id = 1:3, value = c(22, 33, 44), 
                                code = c("943", "943", "3a0"), 
                                product = c("Product 1", "Product 1", "Product 1")),
                           row.names = c(NA,-3L), 
                           class = c("data.table", "data.frame")), 
                 structure(list(id = 1:3, value = c(22, 33, 44), 
                                code = c("943", "94f", "3a0"), 
                                product = c("Product 2", "Product 2", "Product 2")),
                           row.names = c(NA,-3L), 
                           class = c("data.table", "data.frame")),
                 structure(list(id = 1:3, value = c(22, 33, 44), 
                                code = c("977", "943", "3a0"), 
                                product = c("Product 3", "Product 3", "Product 3")),
                           row.names = c(NA,-3L), 
                           class = c("data.table", "data.frame")))

我想删除所有具有多个列表元素且具有相同code 的列表对象。例如,第一个对象[[1]] 有两个条目,其代码为943。我想删除整个对象,只保留那些没有任何重复的对象。

The expected outcome would therefore be:   myList <- list(
    structure(list(id = 1:3, value = c(22, 33, 44), 
                   code = c("943", "94f", "3a0"), 
                   product = c("Product 2", "Product 2", "Product 2")),
              row.names = c(NA,-3L), 
              class = c("data.table", "data.frame")),
    structure(list(id = 1:3, value = c(22, 33, 44), 
                   code = c("977", "943", "3a0"), 
                   product = c("Product 3", "Product 3", "Product 3")),
              row.names = c(NA,-3L), 
              class = c("data.table", "data.frame")))

我正在考虑使用和 lapply,但我无法将其用于 qwork

any(duplicated(myList[[1]]$code))

有什么想法或建议吗?

这似乎是一个相对简单的问题,但我想不通

【问题讨论】:

    标签: r list subset lapply


    【解决方案1】:

    您的代码any(duplicated(myList[[1]]$code))可以在Filter中使用

    Filter(function(x) !any(duplicated(x$code)), myList)
    
    #[[1]]
    #   id value code   product
    #1:  1    22  943 Product 2
    #2:  2    33  94f Product 2
    #3:  3    44  3a0 Product 2
    
    #[[2]]
    #   id value code   product
    #1:  1    22  977 Product 3
    #2:  2    33  943 Product 3
    #3:  3    44  3a0 Product 3
    

    或者purrr

    purrr::keep(myList, ~!any(duplicated(.x$code)))
    purrr::discard(myList, ~any(duplicated(.x$code)))
    

    【讨论】:

      【解决方案2】:

      这行得通吗:

      myList[sapply(lapply(myList, function(x) +duplicated(x$code)), function(x) sum(x) == 0)]
      [[1]]
         id value code   product
      1:  1    22  943 Product 2
      2:  2    33  94f Product 2
      3:  3    44  3a0 Product 2
      
      [[2]]
         id value code   product
      1:  1    22  977 Product 3
      2:  2    33  943 Product 3
      3:  3    44  3a0 Product 3
      

      【讨论】:

        猜你喜欢
        • 2020-09-09
        • 2015-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多