user6644063 user6644063 - 1 month ago 5
R Question

Remove duplicate rows in a data.frame by group

I have got a data.frame (

df
) and I need to remove duplicate rows by groups based on the
df$ID
column.

Here an example of my data.frame:

df = read.table(text = 'day ID
123 1
123 1
123 2
123 2
245 2
245 2
245 3
245 3
412 4
412 6', header = TRUE)


my output should be:

day ID
123 1
123 2
245 2
245 3
412 4
412 6


Any suggestion?

Answer

Try:

require(dplyr)
df %>% group_by(day) %>% filter(ID==ID[1])

that produces, on your last example:

#    day    ID
#  (int) (int)
#1   123     1
#2   123     1
#3   245     2
#4   245     2
#5   412     4
Comments