0

I want to identify duplicates in my data and put the duplicates in a df. My data looks like this:

id   x
1-1  2
1-2  4 
1-2  4
2-1  5
3-1  6 

I want:

id   x   dup
1-1  2   FALSE
1-2  4   TRUE
1-2  4   TRUE 
2-1  5   FALSE
3-1  6   FALSE

I tried:

duplicated(df$ID, incomparables = FALSE)
m1 <- matrix(x, ncol=3059, byrow=TRUE)
d1 <- as.data.frame(m1, stringsAsFactors=FALSE)

However, this isn't working. Can anyone help?

D. Fowler
  • 601
  • 3
  • 7

1 Answers1

0

we need both conditions

df$dup <- with(df, duplicated(id)|duplicated(id, fromLast = TRUE)) 

-output

df
#   id x   dup
#1 1-1 2 FALSE
#2 1-2 4  TRUE
#3 1-2 4  TRUE
#4 2-1 5 FALSE
#5 3-1 6 FALSE

data

df <- structure(list(id = c("1-1", "1-2", "1-2", "2-1", "3-1"), x = c(2L, 
4L, 4L, 5L, 6L)), class = "data.frame", row.names = c(NA, -5L
))
akrun
  • 874,273
  • 37
  • 540
  • 662