AE 13: Rectangling data from the PokéAPI

Application exercise
Modified

March 14, 2024

Packages

We will use the following packages in this application exercise.

  • tidyverse: For data import, wrangling, and visualization.
  • jsonlite: For importing JSON files
library(tidyverse)
library(jsonlite)

Gotta catch em’ all!

Pokémon (also known as Pocket Monsters) is a Japanese media franchise consisting of video games, animated series and films, a trading card game, and other related media.1 The PokéAPI contains detailed information about each Pokémon, including their name, type, and abilities. In this application exercise, we will use a set of JSON files containing API results from the PokéAPI to explore the Pokémon universe.

Importing the data

data/pokedex.json and data/types.json contain information about each Pokémon and the different types of Pokémon, respectively. We will use read_json() to import these files.

pokemon <- read_json(path = "data/pokemon/pokedex.json")
types <- read_json(path = "data/pokemon/types.json")

Your turn: Use View() to interactively explore each list object to identify their structure and the elements contained within each object.

Unnesting for analysis

For each of the exercises below, use an appropriate rectangling procedure to unnest_*() one or more lists to extract the required elements for analysis.

How many Pokémon are there for each primary type?

Your turn: Use each Pokemon’s primary type (the first one listed in the data) to determine how many Pokémon there are for each type, then create a bar chart to visualize the distribution. The chart should label each Pokémon type in both English and Japanese.

Tip

Examine the contents of each list object to determine how the relevant variables are structured so you can plan your approach.

There are (at least) three ways you could approach this problem.

  1. Use unnest_wider() twice to extract the primary type from the pokemon list and generate a frequency count.
  2. Use unnest_wider() and hoist() to extract the primary type from the pokemon list and generate a frequency count.
  3. Use unnest_wider() and unnest_longer() to extract the primary type from the pokemon list and generate a frequency count.

Pick one and have at it!

Note

Fancy a challenge? Label each Pokémon type in both English and Japanese.

# add code here

Which primary type of Pokémon are strongest based on total number of points?

Your turn: Use each Pokémon’s base stats to determine which primary type of Pokémon are strongest based on the total number of points. Create a boxplot to visualize the distribution of total points for each primary type.

Tip

To calculate the sum total of points for each Pokémon’s base stats, there are two approaches you might consider. In either approach you first need to get each Pokémon’s variables into separate columns and extract the primary type.

  1. Use unnest_wider() to extract the base stats, then calculate the sum of the base stats.
  2. Use unnest_longer() to extract the base stats, then calculate the sum of the base stats.
# add code here

Acknowledgments

Footnotes

  1. Source: Wikipedia↩︎