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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With