Looking at the harrypotter package which consists of a list for each of the 7 HP books, I want to create a data frame with a column for Book, Chapter, Text.
Using a for loop you can do this by:
library(dplyr)
library(harrypotter)
titles <- c("Philosopher's Stone", "Chamber of Secrets", "Prisoner of Azkaban",
"Goblet of Fire", "Order of the Phoenix", "Half-Blood Prince",
"Deathly Hallows")
books <- list(philosophers_stone, chamber_of_secrets, prisoner_of_azkaban,
goblet_of_fire, order_of_the_phoenix, half_blood_prince,
deathly_hallows)
series <- tibble()
for(i in seq_along(titles)) {
clean <- tibble(chapter = seq_along(books[[i]]),
text = books[[i]]) %>%
mutate(book = titles[i]) %>%
select(book, everything())
series <- rbind(series, clean)
}
Is there a way to get the above with tibble or data.frame + map_chr()?
The problem I've been having in attempting to do this is that the character vectors and elements are unnamed so I don't have anything to pass as an argument into the purrr functions.