【问题标题】:checking for if exists/in by group across two tables跨两个表检查是否存在/按组
【发布时间】:2017-03-11 14:58:01
【问题描述】:

我有两个相互对应的数据帧列表,例如 newdata_list$SHARKS 和 old_data_list$Sharks。

在每个 list.dataframe 中都有可以进一步组合在一起的数据行。

在每个 new_list.dataframe 和其中的子组中,我想确保我的列中的任何值也存在于相应的 old_list.dataframe 子组中。

如果在对应表中没有找到new_list.dataframe列中的值,我想过滤掉。所有组合都非常匹配。

这里第一列是分组项(动物),而接下来的两列是我检查值是否存在的列。

我期待在 new_list 列中得到答案

示例数据集

OLD_LIST = structure(list(LAND = structure(list(Animal = structure(c(2L,  2L, 1L), .Label = c("Frogs", "Snakes"), class = "factor"), Species = structure(c(2L,  3L, 1L), .Label = c("Green", "Sea", "Tiger"), class = "factor"), 
    Continent = structure(c(3L, 1L, 2L), .Label = c("Africa", 
    "America", "Australia"), class = "factor"), Year = c(2016L, 
    2015L, 2012L)), .Names = c("Animal", "Species", "Continent",  "Year"), class = "data.frame", row.names = c(NA, -3L)), SEA = structure(list(
    Animal = structure(c(1L, 2L, 2L), .Label = c("Shark", "Whale"
    ), class = "factor"), Species = structure(c(3L, 1L, 2L), .Label = c("Blue", 
    "Grey", "Tiger"), class = "factor"), Ocean = structure(c(1L, 
    1L, 1L), .Label = "Atlantic", class = "factor"), Year = c(2014L, 
    2015L, 2015L)), .Names = c("Animal", "Species", "Ocean",  "Year"), class = "data.frame", row.names = c(NA, -3L))), .Names = c("LAND",  "SEA"))

new_list = structure(list(SEA = structure(list(Animal = structure(c(1L,  1L, 2L, 2L, 2L), .Label = c("Shark", "Whale"), class
= "factor"), 
    Species = structure(c(3L, 3L, 1L, 2L, 2L), .Label = c("Blue", 
    "Grey", "Tiger"), class = "factor"), Ocean = structure(c(2L, 
    3L, 2L, 3L, 1L), .Label = c("Arctic", "Atlantic", "Pacific"
    ), class = "factor"), Value = 1:5, Expected_Result = structure(c(1L, 
    2L, 1L, 2L, 2L), .Label = c("Keep", "Remove"), class = "factor")), .Names = c("Animal",  "Species", "Ocean", "Value", "Expected_Result"), class = "data.frame", row.names = c(NA, 
-5L)), LAND = structure(list(Animal = structure(c(2L, 2L, 2L,  2L, 1L), .Label = c("Frogs", "Snakes"), class = "factor"), Species = structure(c(3L,  3L, 3L, 2L, 1L), .Label = c("Dart", "Hammerhead", "Tiger"), class = "factor"), 
    Continent = structure(c(3L, 3L, 2L, 4L, 1L), .Label = c("Arctic", 
    "Atlantic", "Australia", "Pacific"), class = "factor"), Value = 1:5, 
    Expected_Result = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("Keep", 
    "Remove"), class = "factor")), .Names = c("Animal", "Species",  "Continent", "Value", "Expected_Result"), class = "data.frame", row.names = c(NA, 
-5L))), .Names = c("SEA", "LAND"))

【问题讨论】:

  • Snakes Tiger AustraliaOLD_LIST 中不存在,所以第一个返回为空,但通常类似于library(tidyverse) ; new_list %>% sort_by(~names(.x)) %>% map2(OLD_LIST, semi_join)

标签: r group-by dplyr filtering


【解决方案1】:

这应该适用于任意数量的数据帧对:

library(tidyverse)

result <- lst(
  new_list,
  OLD_LIST
) %>% 
  map(sort_by, names) %>% 
  transpose() %>% 
  map(set_names, nm = c("x", "y")) %>% 
  invoke_map(semi_join, .)

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2021-05-21
    相关资源
    最近更新 更多