1

如果两列具有相同的观察结果(分类和分类-1“),我正在尝试创建一个新列来检查组(id 和编号)。

这是原始数据框:

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

我想得到这个:

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

1 回答 1

2

也许这会有所帮助

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

如果我们需要检查元素,那么allunique通过'id','number'分组后,获取unique两者的元素classificationclassification-1检查它们是否相等setequal

reprex %>%
    group_by(id, number) %>%
    mutate(check = setequal(sort(unique(classification)), 
                              sort(unique(`classification-1`))))
于 2019-05-17T14:36:47.760 回答