Trying to extract data between semicolons and put that data into new columns.
Here is some data
df <- data.frame(data = c("a;;c;d", "a;b;;d","a;;;d","a;b;;;"), num =c(1:4))
Here is what I have scraped together so far from S.O.
res <- df %>%
mutate(
colA = str_extract(data, "^[^;]*(?=;)"),
colB = str_extract(data, "(?<=;)[^;]*(?=;)"),
colC = str_extract(data, "(?<=;)(?<=;)[^;]*(?=;)"),
colD = str_extract(data, "(?<=;)[^;]*$")
)
It nearly does what I want but colC is the same as colB. I dont really understand regex so a solution and a explanation would be gratefully received.