如何比较列表元素并在 R 中保留重复项?

问题描述 投票:0回答:0

我有一长串 redditors

str(list_map_descr)
List of 4570
 $ Europa_Teles_BTR    :'data.frame':   1916 obs. of  2 variables:
  ..$ subreddit: chr [1:1916] "portugal" "Warthunder" "Warthunder" "portugal" ...
  ..$ date_utc : chr [1:1916] "2020-05-30" "2020-05-30" "2020-05-30" "2020-05-30" ...
 $ growmylife          :'data.frame':   92 obs. of  2 variables:
  ..$ subreddit: chr [1:92] "PsoriaticArthritis" "Telegram" "google" "Notion" ...
  ..$ date_utc : chr [1:92] "2021-06-27" "2021-01-04" "2020-12-14" "2020-10-01" ...
 $ fzncdata            :'data.frame':   182 obs. of  2 variables:
  ..$ subreddit: chr [1:182] "a:t5_39x4c" "nba" "NEET" "NEET" ...
  ..$ date_utc : chr [1:182] "2019-06-21" "2019-06-11" "2019-06-09" "2019-04-30" ...

以及此列表的转换、过滤版本供我分析。

str(list_map_date_o_2_1)
List of 2132
 $ Europa_Teles_BTR    :'data.frame':   562 obs. of  4 variables:
  ..$ subreddit : chr [1:562] "Warthunder" "Warthunder" "Warthunder" "Warthunder" ...
  ..$ date_utc  : Date[1:562], format: "2020-05-30" "2020-05-30" "2020-05-29" ...
  ..$ Posts_stop: num [1:562] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Posts_game: num [1:562] 1 1 1 1 1 1 1 1 1 1 ...
 $ growmylife          :'data.frame':   37 obs. of  4 variables:
  ..$ subreddit : chr [1:37] "RocketLeague" "DaysGone" "StopGaming" "StopGaming" ...
  ..$ date_utc  : Date[1:37], format: "2020-09-23" "2020-04-04" "2019-10-10" ...
  ..$ Posts_stop: num [1:37] NA NA 1 1 1 1 1 1 1 1 ...
  ..$ Posts_game: num [1:37] 1 1 NA NA NA NA NA NA NA NA ...
 $ fzncdata            :'data.frame':   15 obs. of  4 variables:
  ..$ subreddit : chr [1:15] "DotA2" "GlobalOffensive" "DotA2" "DotA2" ...
  ..$ date_utc  : Date[1:15], format: "2019-03-30" "2019-03-02" "2018-11-28" ...
  ..$ Posts_stop: num [1:15] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Posts_game: num [1:15] 1 1 1 1 1 1 1 1 1 1 ...

我现在想通过新列表的元素过滤我的旧列表 list_map_descr.

我认为列表共享它们的元素名称可能很棘手,但在它们的数据框中有不同的变量,所以首先我只提取了元素的名称

list_map_date_o_2_1

然后我尝试了我能想象的所有版本的 filter、keep、lapply 例如

words <- as.list(names(list_map_date_o_2_1))

这些都不起作用。我认为问题在于我并没有尝试按值进行过滤,而是我想告诉 R 比较两个元素名称,而我无法用我的方法实现这一点。

我希望这样:

list_map_descr_test_3 <- map(list_map_descr, ~filter(words %in% .x))
list_map_descr_test_2 <- map(list_map_descr, ~ filter(.x, .x %in% words))
list_map_descr_test_2 <- map(list_map_descr, ~ keep(any(.x %in% words == TRUE)))
list_map_descr_test_2 <- mapply(function(x, y) x %in% y, list_map_descr, words, SIMPLIFY=FALSE)
list_map_descr_test_2 <- lapply(function(x, y) x %in% y, list_map_descr, words, SIMPLIFY=FALSE)
list_map_descr_test_2 <- purrr::keep(list_map_descr, ~.x %in% words == TRUE)

我非常感谢任何建议!

r dplyr subset purrr
© www.soinside.com 2019 - 2024. All rights reserved.