Which genres have the most Nintendo 64 games in this dataset? I chose the N64 because I wanted to explore video games while practicing Tidyverse functions. This example shows how to use three dplyr functions: filter() to select rows, count() to count them, and arrange() to order the results.
The data
The Video Games Sales dataset contains game-platform records for many consoles. It includes each game’s name, platform, genre, publisher, and estimated sales. The dataset creator describes it as a historical scrape of VGChartz. The source description says it covers games with sales above 100,000 copies, but the CSV also contains entries with lower reported sales, so that threshold is not a reliable rule for every row. The dataset is not a complete list of all video games or N64 releases, so the results below describe only games in this dataset.
The code reads the CSV from its public source. An internet connection is needed to run the document.
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.5.3
Warning: package 'readr' was built under R version 4.5.3
games <-read_csv("https://zenodo.org/records/5898311/files/vgsales.csv?download=1",show_col_types =FALSE)
Let’s look at a few rows and the columns we will use. Platform identifies the console and Genre describes the type of game. The pipe (%>%) passes the data on its left to the next function. select() chooses columns, while head(8) displays the first eight rows.
games %>%select(Name, Platform, Genre) %>%head(8)
# A tibble: 8 × 3
Name Platform Genre
<chr> <chr> <chr>
1 Wii Sports Wii Sports
2 Super Mario Bros. NES Platform
3 Mario Kart Wii Wii Racing
4 Wii Sports Resort Wii Sports
5 Pokemon Red/Pokemon Blue GB Role-Playing
6 Tetris GB Puzzle
7 New Super Mario Bros. DS Platform
8 Wii Play Wii Misc
1. Keep only Nintendo 64 games with filter()
The original dataset includes several platforms. filter() keeps the rows that meet a condition. Here, Platform == "N64" asks R to keep rows whose platform is N64. The two equals signs test whether the values match.
n64_games <- games %>%filter(Platform =="N64")n64_games %>%select(Name, Platform, Genre) %>%head(8)
# A tibble: 8 × 3
Name Platform Genre
<chr> <chr> <chr>
1 Super Mario 64 N64 Platform
2 Mario Kart 64 N64 Racing
3 GoldenEye 007 N64 Shooter
4 The Legend of Zelda: Ocarina of Time N64 Action
5 Super Smash Bros. N64 Fighting
6 Pokemon Stadium N64 Strategy
7 Donkey Kong 64 N64 Platform
8 Diddy Kong Racing N64 Racing
We saved the result as n64_games. The original games dataset still contains the other platforms. We can use nrow() to see how filtering changed the number of records.
Each number is a count of records in this dataset, not a count of games sold. For example, 10 in the Racing row would mean 10 racing game records, not 10 copies sold.
3. Sort the results with arrange()
arrange() orders rows. Wrapping the count in desc() puts the largest counts first.
The first row now shows the genre with the most N64 records in this dataset. In the source version examined for this tutorial, Sports is first with 80 records, followed by Racing with 57. These are counts of records, not units sold or a complete count of all games released. If two genres have the same count, their order does not mean one has more games than the other.
Optional: visualize the counts
A bar chart can make the counts easier to compare. geom_col() uses the counts we already calculated, and coord_flip() turns the chart sideways so genre names have room. We use reorder() inside the plot to order genres by count; sorting a table with arrange() alone does not change the order of categories on a ggplot axis.
ggplot(genre_counts, aes(x =reorder(Genre, number_of_games), y = number_of_games)) +geom_col(fill ="steelblue") +coord_flip() +labs(title ="Nintendo 64 Games by Genre",subtitle ="Counts of records in the historical video game sales dataset",x ="Genre",y ="Number of game records" )
Takeaway
filter() selected N64 records, count() counted those records by genre, and arrange() put the largest counts first. The results are limited to the games included in this historical sales dataset; they should not be interpreted as a complete count of the N64 library.