Replace duplicated elements with NA, instead of removing them

Solution 1:

You were pretty close. I'm not sure what DFl is though. But this works...

DF <- data.frame(A=c("a", "a", "a", "b", "b", "c"))
DF$A[duplicated(DF$A)] <- NA
> DF
     A
1    a
2 <NA>
3 <NA>
4    b
5 <NA>
6    c

Solution 2:

Taking the example from above, if you have a file with several columns, and you want to do the same for each column, you could use this:

 DF <- data.frame(col1=c("a","b","a","c","b","c"),col2=c("b","c","c","c","a","a"))


for(i in 1:ncol(DF)){
  DF[,i][duplicated(DF[,i])] <- NA 
}