Ally Peyton
Ally Peyton

Reputation: 9

Pulling out elements of a list of tibbles after using dplyr group_split (R)

I'm working with a dataset containing the full works of Shakespeare, where each row in the dataframe is a line of a Shakespeare text, and am trying to combine the individual lines of text into one large row of content for each play of Shakespeare's. So far, I've been able to use group_split from dplyr to split the dataframe into tibbles containing the contents of each work, but I can't seem to pull the contents out of the tibbles based on the name of the play.

So far, I've been able to create a large vectors list of tibbles by using the following code:

playtbls <- data %>% 
  group_split(name) 

I'm stuck on how to access anything from the resulting tibbles without repeatedly using indexing formatting. I've been able to pull tcontents of individual tibbles by using the following code, but I know there has to be a faster and cleaner way to do this!

playtbls[[1]][["content"]]

My ultimate goal is to be able to append the contents of these tibbles to a new dataframe "plays" which contains the title and genre of each play, where each play is an individual row. Basically, I need to be able to say for each play's name, when plays$name == playtbls[[x]][["name"]], append playtbls[[x]][["content"]] to the plays dataframe

The data is available from the bardr package.

Upvotes: 0

Views: 424

Answers (1)

akrun
akrun

Reputation: 886938

We can use

lapply(playtbls, `[[`, "content")

Or with tidyverse

library(purrr)
library(dplyr)
map(playtbls, ~ .x %>%
              pluck(content))

Upvotes: 1

Related Questions