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

huangapple go评论101阅读模式
英文:

How to compare list elements and keep duplicates?

问题

我有一个庞大的redditor列表

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

和一个经过转换和筛选的列表,用于我的分析。

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

我现在想要通过新列表list_map_date_o_2_1的元素来过滤我的旧列表list_map_descr

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

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

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

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

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

我期望这样:

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

对于任何建议,我都非常感激!

英文:

I have a big list of redditors

  1. str(list_map_descr)
  2. List of 4570
  3. $ Europa_Teles_BTR :&#39;data.frame&#39;: 1916 obs. of 2 variables:
  4. ..$ subreddit: chr [1:1916] &quot;portugal&quot; &quot;Warthunder&quot; &quot;Warthunder&quot; &quot;portugal&quot; ...
  5. ..$ date_utc : chr [1:1916] &quot;2020-05-30&quot; &quot;2020-05-30&quot; &quot;2020-05-30&quot; &quot;2020-05-30&quot; ...
  6. $ growmylife :&#39;data.frame&#39;: 92 obs. of 2 variables:
  7. ..$ subreddit: chr [1:92] &quot;PsoriaticArthritis&quot; &quot;Telegram&quot; &quot;google&quot; &quot;Notion&quot; ...
  8. ..$ date_utc : chr [1:92] &quot;2021-06-27&quot; &quot;2021-01-04&quot; &quot;2020-12-14&quot; &quot;2020-10-01&quot; ...
  9. $ fzncdata :&#39;data.frame&#39;: 182 obs. of 2 variables:
  10. ..$ subreddit: chr [1:182] &quot;a:t5_39x4c&quot; &quot;nba&quot; &quot;NEET&quot; &quot;NEET&quot; ...
  11. ..$ date_utc : chr [1:182] &quot;2019-06-21&quot; &quot;2019-06-11&quot; &quot;2019-06-09&quot; &quot;2019-04-30&quot; ...

and a transformed, filtered version of this list for my analysis.

  1. str(list_map_date_o_2_1)
  2. List of 2132
  3. $ Europa_Teles_BTR :&#39;data.frame&#39;: 562 obs. of 4 variables:
  4. ..$ subreddit : chr [1:562] &quot;Warthunder&quot; &quot;Warthunder&quot; &quot;Warthunder&quot; &quot;Warthunder&quot; ...
  5. ..$ date_utc : Date[1:562], format: &quot;2020-05-30&quot; &quot;2020-05-30&quot; &quot;2020-05-29&quot; ...
  6. ..$ Posts_stop: num [1:562] NA NA NA NA NA NA NA NA NA NA ...
  7. ..$ Posts_game: num [1:562] 1 1 1 1 1 1 1 1 1 1 ...
  8. $ growmylife :&#39;data.frame&#39;: 37 obs. of 4 variables:
  9. ..$ subreddit : chr [1:37] &quot;RocketLeague&quot; &quot;DaysGone&quot; &quot;StopGaming&quot; &quot;StopGaming&quot; ...
  10. ..$ date_utc : Date[1:37], format: &quot;2020-09-23&quot; &quot;2020-04-04&quot; &quot;2019-10-10&quot; ...
  11. ..$ Posts_stop: num [1:37] NA NA 1 1 1 1 1 1 1 1 ...
  12. ..$ Posts_game: num [1:37] 1 1 NA NA NA NA NA NA NA NA ...
  13. $ fzncdata :&#39;data.frame&#39;: 15 obs. of 4 variables:
  14. ..$ subreddit : chr [1:15] &quot;DotA2&quot; &quot;GlobalOffensive&quot; &quot;DotA2&quot; &quot;DotA2&quot; ...
  15. ..$ date_utc : Date[1:15], format: &quot;2019-03-30&quot; &quot;2019-03-02&quot; &quot;2018-11-28&quot; ...
  16. ..$ Posts_stop: num [1:15] NA NA NA NA NA NA NA NA NA NA ...
  17. ..$ Posts_game: num [1:15] 1 1 1 1 1 1 1 1 1 1 ...

I want to filter now my old list list_map_descr by the elements of the new list list_map_date_o_2_1.

I thought it could be tricky that the lists share their element names, but have different variables in their data frames, so first I extracted only the names of the elements

  1. words &lt;- as.list(names(list_map_date_o_2_1))

and then I tried all versions of filter, keep, lapply that I could imagine e.g.

  1. list_map_descr_test_3 &lt;- map(list_map_descr, ~filter(words %in% .x))
  2. list_map_descr_test_2 &lt;- map(list_map_descr, ~ filter(.x, .x %in% words))
  3. list_map_descr_test_2 &lt;- map(list_map_descr, ~ keep(any(.x %in% words == TRUE)))
  4. list_map_descr_test_2 &lt;- mapply(function(x, y) x %in% y, list_map_descr, words, SIMPLIFY=FALSE)
  5. list_map_descr_test_2 &lt;- lapply(function(x, y) x %in% y, list_map_descr, words, SIMPLIFY=FALSE)
  6. list_map_descr_test_2 &lt;- purrr::keep(list_map_descr, ~.x %in% words == TRUE)

None of this worked. I think the problem is that I am not trying to filter by a value, instead I want to tell R to compare both element names and this I can not implement with my approaches.

I would expect this:

  1. str(list_map_descr_test_3)
  2. List of 2132
  3. $ Europa_Teles_BTR :&#39;data.frame&#39;: 1916 obs. of 2 variables:
  4. ..$ subreddit: chr [1:1916] &quot;portugal&quot; &quot;Warthunder&quot; &quot;Warthunder&quot; &quot;portugal&quot; ...
  5. ..$ date_utc : chr [1:1916] &quot;2020-05-30&quot; &quot;2020-05-30&quot; &quot;2020-05-30&quot; &quot;2020-05-30&quot; ...
  6. $ growmylife :&#39;data.frame&#39;: 92 obs. of 2 variables:
  7. ..$ subreddit: chr [1:92] &quot;PsoriaticArthritis&quot; &quot;Telegram&quot; &quot;google&quot; &quot;Notion&quot; ...
  8. ..$ date_utc : chr [1:92] &quot;2021-06-27&quot; &quot;2021-01-04&quot; &quot;2020-12-14&quot; &quot;2020-10-01&quot; ...
  9. $ fzncdata :&#39;data.frame&#39;: 182 obs. of 2 variables:
  10. ..$ subreddit: chr [1:182] &quot;a:t5_39x4c&quot; &quot;nba&quot; &quot;NEET&quot; &quot;NEET&quot; ...
  11. ..$ date_utc : chr [1:182] &quot;2019-06-21&quot; &quot;2019-06-11&quot; &quot;2019-06-09&quot; &quot;2019-04-30&quot; ...

I am very grateful for any suggestion!

答案1

得分: 1

  1. 我们可以使用

library(dplyr)
nm1 <- intersect(names(list_map_descr), names(list_map_date_o_2_1))
list_map_new <- list_map_descr[nm1]

  1. <details>
  2. <summary>英文:</summary>
  3. We may use

library(dplyr)
nm1 <- intersect(names(list_map_descr), names(list_map_date_o_2_1))
list_map_new <- list_map_descr[nm1]

huangapple
  • 本文由 发表于 2023年2月19日 22:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定