如何在数据框中的某些条件下检查重复?

How to check for duplication under some conditions in a data frame?

我有一个具有给定结构的数据框

 'data.frame':  3005 obs. of  6 variables:
 $ trial_id   : int  2184 2184 2184 2184 2184 2184 2184 2184 2184 2184 ...
 $ ctri_number: Factor w/ 278 levels "CTRI/2016/06/006993  ",..: 134 134 134 134 134 134 134 134 134 134 ...
 $ noofsites  : int  13 13 13 13 13 13 13 13 13 13 ...
 $ Sites_Names: chr  "Acharya Tulsi Regional Cancer Treatment And Research Institute" "City Cancer Centre" "Curie Manavata Cancer Centre" "Government Stanley Medical College and Hospital" ...
 $ noofcom    : int  13 13 13 13 13 13 13 13 13 13 ...
 $ ECs_Names  : Factor w/ 2493 levels "\"Aakash Healthcare Institutional Ethics Committee Aakash Healthcare Super Specialty Hospital Hospital Plot, Ro"| __truncated__,..: 218 210 211 1007 834 859 2047 2058 2096 2212 ...

共有 278 个独特的trial_ids。 每个试验都有超过 1 个 noofsite,因此它们各自的名称,每个站点名称在不同的行中。因此对于每次试验,行数 = noofsites。

我想检查 - 对于每个单独的试验,是否有 Sites_Names 的重复。我不想检查整个数据框中站点名称的重复,只针对特定试验。 如何实现???

提前致谢

也许您想 group_by(trial_id) 在检查重复项之前,例如

library(tidyverse)

df1 <- mtcars %>%
  as_tibble() %>% 
  add_count(disp, name = "duplicates?")
df1
# A tibble: 32 x 12
#     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb `duplicates?`
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>         <int>
# 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4             2
# 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4             2
# 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1             1
# 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1             1
# 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2             2
# 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1             1
# 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4             2
# 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2             1
# 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2             1
#10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4             2

df2 <- mtcars %>% 
  group_by(carb) %>% 
  add_count(disp, name = "duplicates?") %>% 
  ungroup()
df2
# A tibble: 32 x 12
#     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb `duplicates?`
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>         <int>
# 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4             2
# 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4             2
# 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1             1
# 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1             1
# 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2             1
# 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1             1
# 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4             1
# 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2             1
# 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2             1
#10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4             2

identical(df1, df2)
# FALSE

在这里,重复项在每个小标题的最后一列中注明。如果您 group_by(carb) 在检查重复项之前,与在整个数据帧中搜索重复项相比,您获得的重复项更少(df1 和 df2 不相同)。 IE。数据集按“carb”分组,每个组分别搜索重复项。这是否解决了您的问题?