I have a dataframe containing two columns:
an
idstring: which is a text string column, containing repeated text elements separated by the symbols/library(tidyverse) df_input <- data.frame(stringsAsFactors=FALSE, id = c(123, 234, 345, 456), string = c("[\"aaa\"] / [\"aaa\"] / [\"aaa\"] / bbb / bbb / bbb", "[\"hello hello\"] / [\"hello hello\"] / [\"hello hello\"] / [\"hello hello\"]", "my name is tim / my name is tim / my name is tim", "[\"hello word\"]") )
Looking like:
id string
1 123 ["aaa"] / ["aaa"] / ["aaa"] / bbb / bbb / bbb
2 234 ["hello hello"] / ["hello hello"] / ["hello hello"] / ["hello hello"]
3 345 my name is tim / my name is tim / my name is Tim
4 456 ["hello word"]
The pattern I see is that each time there is a group of repeated elements, it is separated by the symbol /:
["aaa"] / ["aaa"] / ["aaa"] / bbb / bbb / bbb
Or:
my name is tim / my name is tim / my name is Tim
But also cases with a single element:
["hello word"]
I would like to have a dataframe like the following:
df_output <- data.frame(stringsAsFactors=FALSE,
id = c(123, 234, 345, 456),
string = c("[\"aaa\"] / bbb", "[\"hello hello\"]", "my name is tim",
"[\"hello word\"]")
)
Where:
id string
1 123 ["aaa"] / bbb
2 234 ["hello hello"]
3 345 my name is tim
4 456 ["hello word"]
and I keep only the unique elements; if multiple elements are present, they are separated by /.
Any solution in dplyr?