0
col1 col2
First,First,Second row,First,First
Second,Second,Third row,Second,Second

I would like to transform col1 into to this, without removing duplicates in col2 and without creating new rows

col1 col2
First,Second row,First,First
Second,Third row,Second,Second

And what if the separator is a || instead of a ,?

2 Answers2

0

One option using tidyr and dplyr

dat %>% separate_rows(col1) %>% distinct(col1, col2)
# A tibble: 2 × 2
  col1   col2             
  <chr>  <chr>            
1 First  row,First,First  
2 Second row,Second,Second
user438383
  • 4,338
  • 6
  • 23
  • 35
-1

You could split the first column on comma, to generate a vector. Then, use unique() and finally recreate the CSV:

df$col1 <- paste(unique(strsplit(df$col1, ",")[[1]]), collapse=",")
Tim Biegeleisen
  • 451,927
  • 24
  • 239
  • 318