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
  • 5,716
  • 8
  • 28
  • 43
  • I do not want to create new rows. Mantain existing ones. What if we have in Col1 something like c(First,First,Second) and c(Second,Second,Third). The data frame will increase in number of rows – Karlos Garcia Jan 19 '22 at 10:53
  • @Karlos can you update the data in your question and I will update my answer. – user438383 Jan 19 '22 at 11:07
0

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
  • 502,043
  • 27
  • 286
  • 360