根据 R 中的条件对嵌套列表进行子集化

subsetting a nested list based on a conidtion in R

我的嵌套列表如下所示:

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

有什么想法或建议吗?

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

这个有用吗:

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

您的代码 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)))