Edit: This was marked as a duplicate. It is not. The question here is not only about splitting a single column into multiple ones, as my separate code would had worked. The main point of my question is splitting the column when the row string possess varying lengths of column output.
I'm trying to turn this:
data <- c("Place1-Place2-Place2-Place4-Place2-Place3-Place5",
"Place7-Place7-Place7-Place7-Place7-Place7-Place7-Place7",
"Place1-Place1-Place1-Place1-Place3-Place5",
"Place1-Place4-Place2-Place3-Place3-Place5-Place5",
"Place6-Place6",
"Place1-Place2-Place3-Place4")
Into this:
X1 X2 X3 X4 X5 X6 X7 X8
1 Place1 Place2 Place2 Place4 Place2 Place3 Place5
2 Place7 Place7 Place7 Place7 Place7 Place7 Place7 Place7
3 Place1 Place1 Place1 Place1 Place3 Place5
4 Place1 Place4 Place2 Place3 Place3 Place5 Place5
5 Place6 Place6
6 Place1 Place2 Place3 Place4
I tried to use tidyr's seperate function using this code:
library(data.table)
data <- as.data.table(data)
data_table <- tidyr::separate(data,
data,
sep="-",
into = strsplit(data$data, "-"),
fill = "right")
Sadly I'm getting this error:
Warning message:
Too many values at 3 locations: 1, 2, 4
What do I need to change to make it work?