Home > Mobile >  How to concatenate multiple columns in one and remove duplicates?
How to concatenate multiple columns in one and remove duplicates?

Time:01-07

I have a dataframe like this one:

A <- c("a", "a", "a", "a")
B <- c("b", "b", "b", "b")
C <- c("c", "a", "c", "c")
D <- c("d", "b", "a", "d")
E <- c("a", "a", "b", "e")
F <- c("b", "b", "c", "f")
G <- c("c", "a", "a", "g")
df <- data.frame(A, B, C, D, E, F, G)

I need to merge all values from the columns A to G, remove duplicates, and store a resulting list in a new column. So, the final result should look like this:

enter image description here

CodePudding user response:

Try this one

> df$new <- apply(df,1,unique)
> df
  A B C D E F G                 new
1 a b c d a b c          a, b, c, d
2 a b a b a b a                a, b
3 a b c a b c a             a, b, c
4 a b c d e f g a, b, c, d, e, f, g

CodePudding user response:

A possible solution:

library(tidyverse)

A <- c("a", "a", "a", "a")
B <- c("b", "b", "b", "b")
C <- c("c", "a", "c", "c")
D <- c("d", "b", "a", "d")
E <- c("a", "a", "b", "e")
F <- c("b", "b", "c", "f")
G <- c("c", "a", "a", "g")
df <- data.frame(A, B, C, D, E, F, G)

df %>% 
  rowwise %>% 
  mutate(new = c_across(everything()) %>% unique %>% str_c(collapse = ",")) %>% 
  ungroup

#> # A tibble: 4 × 8
#>   A     B     C     D     E     F     G     new          
#>   <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>        
#> 1 a     b     c     d     a     b     c     a,b,c,d      
#> 2 a     b     a     b     a     b     a     a,b          
#> 3 a     b     c     a     b     c     a     a,b,c        
#> 4 a     b     c     d     e     f     g     a,b,c,d,e,f,g

CodePudding user response:

this is sort of a silly way of doing it, but does this address your issue?

list(unique(t(df)[,1]), 
 unique(t(df)[,2]),
 unique(t(df)[,3]),
 unique(t(df)[,4]))
  •  Tags:  
  • Related