VincentLin
VincentLin

Reputation: 361

Unite columns with unique values

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?

Upvotes: 3

Views: 1114

Answers (3)

akrun
akrun

Reputation: 887088

We change the columns to character class, use pmap (from purrr) to get the unique elements per row and paste it together

library(tidyverse)
test %>% 
  mutate_all(as.character) %>% 
  pmap_chr(~ c(...) %>% 
                 unique %>% 
                 toString) %>% 
  bind_cols(new_col = ., test)
# A tibble: 3 x 4
#  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  

Upvotes: 0

YOLO
YOLO

Reputation: 21709

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

test$new_col <- apply(test, 1, function(x) paste(unique(x), collapse = ','))

Upvotes: 4

Aleksandr
Aleksandr

Reputation: 1914

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 = ","))

Upvotes: 4

Related Questions