I’ve been encountering lists of data frames both at work and at play. Most of the time, I need only bind them together
with dplyr::bind_rows()
or purrr::map_df()
. But recently I’ve needed to join them by a shared key. This operation is
more complex. In fact, I admitted defeat earlier this year when I allowed rcicero::get_official()
to return a list of data frames rather than
a single, tidy table. Forgiveable at the time, but now I know better.
For a quick demonstration, let’s get our list of data frames:
Now we have a list of data frames that share one key column: “A”. I needed some programmatic way to join each data frame to the next,
and while cycling through abstractions, I recalled the reduce function from Python, and I was ready to bet my life R had something similar. And we do:
for basers, there’s Reduce()
, but for civilized, tidyverse folk there’s purrr::reduce()
.
Here’s how to create and merge df_list
together with base R and Reduce()
:
Hideous, right?! Behold the glory of the tidyverse:
There’s just no comparison. I need to go back and implement this little trick in rcicero pronto.
By way of conclusion, here’s an example from my maxprepsr package that I’ve since learned violates CBS Sports’ Terms of Use. Don’t do this, but here’s the idea:
That is quite a bit of power with just a dash of tidyverse piping.