识别R中特定列中df中的重复项

时间:2018-06-20 15:43:00

标签: r

对于示例数据框:

df <- structure(list(code = c("a1", "a1", "b2", "v4", "f5", "f5", "h7", 
                        "a1"), name = c("katie", "katie", "sally", "tom", "amy", "amy", 
                                        "ash", "james"), number = c(3.5, 3.5, 2, 6, 4, 4, 7, 3)), .Names = c("code", 
                                                                                                             "name", "number"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                        -8L), spec = structure(list(cols = structure(list(code = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                             "collector")), name = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                                                               "collector")), number = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                   "collector"))), .Names = c("code", "name", "number")), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "collector"))), .Names = c("cols", "default"), class = "col_spec"))

我想生成一个仅在一个特定列中具有重复项的行的数据框。

我知道我可以做到:

df[duplicated(df),]

但是对于我特定的较大的实际数据框,我只想指定要突出显示重复项的特定列。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

duplicated()接受矢量...

df[duplicated(df$name), ]
  code  name number
2   a1 katie    3.5
6   f5   amy    4.0