Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace duplicated elements with NA, instead of removing them

Tags:

r

I have a DF. One of its columns looks like

DF$A
A
    a
    a
    a
    b
    b
    b
    c
    c

I am trying to replace all duplicated characters in this column with NA. Naively, I tried

DF$A <- DFl[duplicated(DF$A),] <- NA

But it just converts whole DF to NA values. Thank you for any suggestion.

like image 381
HoHoHo Avatar asked May 18 '16 17:05

HoHoHo


People also ask

How to get rid of repeating values in R?

Remove Duplicate rows in R using Dplyr – distinct () function. Distinct function in R is used to remove duplicate rows in R using Dplyr package. Dplyr package in R is provided with distinct() function which eliminate duplicates rows with single variable or with multiple variable.


1 Answers

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
like image 96
cory Avatar answered Dec 03 '22 03:12

cory