Remove duplicated rows using dplyr
dplyr, r
Solution
Note: `dplyr` now contains the `distinct` function for this purpose.
Original answer below:
library(dplyr)
set.seed(123)
df <- data.frame(
x = sample(0:1, 10, replace = T),
y = sample(0:1, 10, replace = T),
z = 1:10
)
One approach would be to group, and then only keep the first row:
df %>% group_by(x, y) %>% filter(row_number(z) == 1)
## Source: local data frame [3 x 3]
## Groups: x, y
##
## x y z
## 1 0 1 1
## 2 1 0 2
## 3 1 1 4
(In dplyr 0.2 you won't need the dummy `z` variable and will just be able to write `row_number() == 1`)
I've also been thinking about adding a `slice()` function that would work like:
df %>% group_by(x, y) %>% slice(from = 1, to = 1)
Or maybe a variation of `unique()` that would let you select which variables to use:
df %>% unique(x, y)
Problem
I have a data.frame like this - ``` set.seed(123) df = data.frame(x=sample(0:1,10,replace=T),y=sample(0:1,10,replace=T),z=1:10) > df x y z 1 0 1 1 2 1 0 2 3 0 1 3 4 1 1 4 5 1 0 5 6 0 1 6 7 1 0 7 8 1 0 8 9 1 0 9 10 0 1 10 ``` I would like to remove duplicate rows based on first two columns. Expected output - ``` df[!duplicated(df[,1:2]),] x y z 1 0 1 1 2 1 0 2 4 1 1 4 ``` I am specifically looking for a solution using `dplyr` package.