Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate values in a single row in dataframe

df <- data.frame(label = c("a","b","c"),
                 val=c("x","b","c"),
                 val1=c("z","b","d"))

   label val val1
1     a   x    z
2     b   b    b
3     c   c    d

I want find out the duplicate values in each row. for 1st row, there is no duplicate for 2nd row , "b" is duplicate for 3rd row, "c" is duplicate. How to find this duplicate in R programming.

Also I need to replace the duplicate elements with NA value.

like image 710
Senthil Kumar Avatar asked Jan 28 '23 06:01

Senthil Kumar


1 Answers

Using duplicated with apply

apply(df,1,duplicated)
      [,1]  [,2]  [,3]
[1,] FALSE FALSE FALSE
[2,] FALSE  TRUE  TRUE
[3,] FALSE  TRUE FALSE

And replace it with NA

df[t(apply(df,1,duplicated))]=NA
df
  label  val val1
1     a    x    z
2     b <NA> <NA>
3     c <NA>    d
like image 167
BENY Avatar answered Jan 31 '23 09:01

BENY