Emu*_*Emu 5 r conditional-formatting subset duplicates dataframe
我想找到两个(或更多)行具有相同的 x,y (位置)但 ID 不同的位置。
在下表中,我只想了解最后两行。
| X | y | ID |
|---|---|---|
| 1 | 2 | 1 |
| 1 | 2 | 1 |
| 1 | 3 | 4 |
| 2 | 3 | 1 |
| 2 | 3 | 2 |
# example data
x <- read.table(text = "x y id
1 2 1
1 2 1
1 3 4
2 3 1
2 3 2", header = TRUE)
Run Code Online (Sandbox Code Playgroud)
另一种方式,使用dplyr:
x %>%
group_by(x, y) %>%
filter(n_distinct(id) > 1)
# A tibble: 2 x 3
# Groups: x, y [1]
x y id
<int> <int> <int>
1 2 3 1
2 2 3 2
Run Code Online (Sandbox Code Playgroud)