rudeboybert / fivethirtyeight

R package of data and code behind the stories and interactives at FiveThirtyEight
https://fivethirtyeight-r.netlify.app/
Other
454 stars 104 forks source link

Replace all tidyr::gather() code in help files with pivot_longer() #55

Closed rudeboybert closed 4 years ago

rudeboybert commented 4 years ago

Some of the help/man files have roxygen2 examples that convert data as originally given in 538's GitHub to "tidy" format using tidyr::gather(). However, tidyr now includes much more intuitively-named and easier to use functions pivot_longer() and pivot_wider(). See this tidyverse.org blog post for more information.

In the case of fivethirtyeight package, we should replace all gather() code with pivot_longer(). For example, if you run ?drinks you'll currently see in the examples:

library(dplyr)
library(tidyr)
library(stringr)
drinks_tidy <- drinks %>%
  gather(type, servings, -c(country, total_litres_of_pure_alcohol)) %>%
  mutate(
    type = str_sub(type, start=1, end=-10)
  ) %>%
  arrange(country, type)

This needs to be updated with:

library(fivethirtyeight)
library(dplyr)
library(tidyr)
library(stringr)
drinks_tidy <- drinks %>%
  pivot_longer(cols = ends_with("servings"), names_to = "type", values_to = "servings") %>%
  mutate(type = str_sub(type, start=1, end=-10)) %>%
  arrange(country, type)

A search of gather in all R/data_X.R files will locate the roxygen2 code for all such cases. For example, the roxygen2 code for drinks is in data_albert.R.

@beanumber Let me know if you have someone who can do this, and I'll explicitly make them an assignee.