组上两列的测试条件

时间:2019-05-17 14:32:12

标签: r dplyr reprex

我正在尝试创建一个新列,以检查两个组(id和number)是否具有相同的观察值(分类和分类-1“)。

这是原始数据帧:

LINKEDSERVER\LINKED.

我想要这个:

reprex <- tribble(~"id",    ~"number",  ~"year",   ~"classification",          ~"classification-1",
                  5,        7020,    2015,    "Trading de servicios",    "Servicios empresariales",
                  2,        4649,    2015,                 "Trading",                  "Comercial",
                  2,        4649,    2015,               "Comercial",                    "Trading",
                  2,        4649,    2016,                 "Trading",                  "Comercial",
                  2,        4649,    2016,               "Comercial",                    "Trading",
                  3,        4651,      2015,                   "Trading",                    "Comercial",
                  3,        4651,      2015,                   "Trading",                   "Comisiones",
                  3,        4651,      2015,                 "Comercial",                      "Trading",
                  3,        4651,      2015,                 "Comercial",                   "Comisiones")

1 个答案:

答案 0 :(得分:1)

也许这会有所帮助

library(dplyr)
reprex %>%
    group_by(id, number) %>% 
    mutate(check = length(intersect(classification, `classification-1`)) > 0)

如果我们需要检查allunique元素,则按'id','number'分组后,获得两个unique的{​​{1}}个元素, classification,检查它们是否等于classification-1

setequal