Eric
Eric

Reputation: 1389

Remove duplicate elements by row in a data frame

I need to replace duplicate elements to NA by row from a data frame. I will take base, tidyverse or data.table solutions. Thank you. Example:

library(tibble)
#input data.frame
tribble(
  ~x, ~y,  ~z,
  1,   2,   3,
  1,   1,   NA,
  4,   1,   4,
  2,   2,   3
)

#desired output
tribble(
  ~x, ~y,  ~z,
  1,   2,   3,
  1,   NA,   NA,
  4,   1,   NA,
  2,   3,   NA
)

Upvotes: 2

Views: 209

Answers (1)

akrun
akrun

Reputation: 886938

Here is a base R option where we loop through the rows, replace the duplicated elements with NA and concatenate (c) the non-NA elements with the NA elements, transpose (t) and assign the output back to the original dataset

df1[] <- t(apply(df1, 1, function(x) {
        x1 <- replace(x, duplicated(x), NA)
        c(x1[!is.na(x1)], x1[is.na(x1)])
        }))
df1
# A tibble: 4 x 3
#       x     y     z
#  <dbl> <dbl> <dbl>
#1     1     2     3
#2     1    NA    NA
#3     4     1    NA
#4     2     3    NA

Upvotes: 2

Related Questions