Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unite columns with unique values

Tags:

r

dplyr

Say I have a dataframe like this:

test <- data.frame(x = c('v01','v02','v03'),
                   y = c('v01','v05','v06'),
                   z = c('v03', 'v03','v08'))

I can unite x:z columns by tidyverse pacakge:

test %>% 
  as.tbl %>%
  unite(new_col, x:y:z, sep = ',', remove = F)

and here is the result:

  new_col     x     y     z    
  <chr>       <fct> <fct> <fct>
1 v01,v01,v03 v01   v01   v03  
2 v02,v05,v03 v02   v05   v03  
3 v03,v06,v08 v03   v06   v08 

but what I desire is unique values, like first row only has 'v01, v03':

  new_col     x     y     z    
  <chr>       <fct> <fct> <fct>
1 v01,v03     v01   v01   v03  
2 v02,v05,v03 v02   v05   v03  
3 v03,v06,v08 v03   v06   v08 

Any help?

like image 813
VincentLin Avatar asked Sep 03 '25 15:09

VincentLin


2 Answers

Take your new column and split it by sep = ",". Then grab only unique elements ant paste it:

test <- data.frame(x = c('v01','v02','v03'),
                   y = c('v01','v05','v06'),
                   z = c('v03', 'v03','v08'))

test = test %>% unite(new_col, x:z, sep = ',', remove = F)
test$new_col = sapply(strsplit(test$new_col, ","), 
                      function(x) new_col = paste(unique(x), collapse = ","))
like image 163
Aleksandr Avatar answered Sep 05 '25 07:09

Aleksandr


Another way, you can do in one line without using unite:

test$new_col <- apply(test, 1, function(x) paste(unique(x), collapse = ','))
like image 30
YOLO Avatar answered Sep 05 '25 07:09

YOLO