I have a column in my dataset where there is a string of character that I want to split.
df = data.frame(col = c("BrBkRY","BBkRBr","YBRG","RBBk"))
This is the vector that I want to use to conditionally split.
sep = c("Br","Bk","R","Y","B","G")
This is what it should look like in the end. I did that by hand.
df2 = data.frame(col = c("BrBkRY","BBkRBr","YBRG","RBBk"),
col1 = c("Br","B","Y","R"),
col2 = c("Bk","Bk","B","B"),
col3 = c("R","R","R","Bk"),
col4 = c("Y","Br","G",""))
df2
col col1 col2 col3 col4
1 BrBkRY Br Bk R Y
2 BBkRBr B Bk R Br
3 YBRG Y B R G
4 RBBk R B Bk
I was thinking using a regex, but usually, you need a splitting character like a .
or -
. But with a string based on character, I don't know. Moreover, don't want to split BkB in B, k and B. But I do want to separate it in Bk and B. Is there a package that can do this ?