R: How to remove duplicated entry across columns within each row

ltong

I have a dataframe that looks like the following. Within each row, I would like to remove entries in X1:n that are duplicate entries.

> df <- data.frame(ID = c("100", "101", "102"),
+                  X1 = c("C23.2", "C23.2", "A79.1"), 
+                  X2 = c("C23.2", NA, "A79.1"),
+                  X3 = c("A19.2", NA, "A79.1"))

The output would look something like this

   ID    X2    X3    X4
1 100 C23.2 A19.2  <NA>
2 101 C23.2  <NA>  <NA>
3 102 A79.1  <NA>  <NA>
Quinten

Using pmap_dfr from purrr:

library(dplyr)
library(purrr)
df %>%
  pmap_dfr(., ~c(...) %>% replace(., duplicated(.), NA)) %>%
  bind_cols(select(df), .)

Output:

   ID    X1   X2    X3
1 100 C23.2 <NA> A19.2
2 101 C23.2 <NA>  <NA>
3 102 A79.1 <NA>  <NA>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find and remove duplicated observations across two columns in R

Find and remove duplicated observations across two columns in R

how to find duplicated columns in row in R?

R remove duplicated columns

Find if there are duplicated values across each row

Keep the max value of each row across columns in R dplyr

gsub across each row of multiple columns of dataframe R

How do I remove duplicated columns from a data frame in R?

How to replace values of several columns based on/ another column in R within each row?

How to assign categories to columns in R so that values within those categories can be summed for each row?

Replace duplicates with NAs within a row across columns

R: detect duplicated row, and find out the count of each duplicated group

How to remove a specific, duplicated line within a file?

Conditional sums based on the columns are duplicated (by row) in R

Identify remove and count duplicated columns in R

Is there a way to remove almost duplicated columns in R

R remove row that are not duplicated from a data frame

Remove duplicated value in a same row in R

Remove row based on sum of numpy array within each entry in df column

Calculate for each row for how many columns is observation within top X%

Append value/index for each duplicated row within a Pandas Dataframe

Compute rowMeans across different columns in each row

remove duplicated pattern/entries within each field in CSV file

R: find rows in data frame within range of each other across multiple columns

pandas remove the duplicated row base on same columns values

How to calculate percetage of value within each group across multiple columns using for-loop

Remove duplicated columns

Remove Duplicated String in a Row

Remove Second Duplicated Row