I am looking to separate a column containing text data into 2 columns, but the separator management is quite tricky and I am convinced there is a regex solution, but not well versed in it to find a way. The dataset sample is:
Obs Message
1 "a : 3 b : 5"
2 "c : 4 a : 2 d : 9"
3 ""
4 "b : 3"
Data chunks are separated by spaces, and variables / values are separated by " : "
my attempt at doing this:
library (tidyr)
data %>% separate(Message, sep= " : ", into = c("variable","value"))
>
Obs variable value
1 1 a 3 b
2 2 c 4 a
3 3 <NA>
4 4 b 3
needs extra steps as the variable length of the message throws off the logic.
If someone please take a look and let me know if any regex (or other approach) would help. Appreciate your input on this.
edit: adding expected output:
Obs Variable Value
1 "a" 3
1 "b" 5
2 "c" 4
2 "a" 2
2 "d" 9
3 "" ""
4 "b" 3