I have a data frame/tibble where I've imported a file of plain text (txt). The text very consistent and is grouped by chapter. Sometimes the chapter text is only one row, sometimes it's multiple row. Data is in one column like this:
# A tibble: 10,708 x 1
x
<chr>
1 "Chapter 1 "
2 "Chapter text. "
3 "Chapter 2 "
4 "Chapter text. "
5 "Chapter 3 "
6 "Chapter text. "
7 "Chapter text. "
8 "Chapter 4 "
I'm trying to clean the data to have a new column for Chapter and the text from each chapter in another column, like this:
# A tibble: 10,548 x 2
x Chapter
<chr> <chr>
1 "Chapter text. " "Chapter 1 "
2 "Chapter text. " "Chapter 2 "
3 "Chapter text. " "Chapter 3 "
4 "Chapter text. " "Chapter 4 "
I've been trying to use regex to split the and group the data at each occurance of the word 'Chapter #' (chapter followed by a number, but cannot get the result I want. Any advice is much appreciated.