head(games)
str(games)STAT120 - Statistical Computing in R
Homework 4
Part I
The dataset used for this part is Video_Games_Sales_as_at_22_Dec_2016.csv. You will practicing practice tidyverse functions for data manipulation.
- Import the dataset into R and call it games. We have been using the following functions to examine basic information about the dataset. Write the pipe version of the same code (i.e., use the same functions, but use the pipe operator to call each function).
Show solution
games %>% head() Name Platform Year_of_Release Genre Publisher
1 Wii Sports Wii 2006 Sports Nintendo
2 Super Mario Bros. NES 1985 Platform Nintendo
3 Mario Kart Wii Wii 2008 Racing Nintendo
4 Wii Sports Resort Wii 2009 Sports Nintendo
5 Pokemon Red/Pokemon Blue GB 1996 Role-Playing Nintendo
6 Tetris GB 1989 Puzzle Nintendo
NA_Sales EU_Sales JP_Sales Other_Sales Global_Sales Critic_Score Critic_Count
1 41.36 28.96 3.77 8.45 82.53 76 51
2 29.08 3.58 6.81 0.77 40.24 NA NA
3 15.68 12.76 3.79 3.29 35.52 82 73
4 15.61 10.93 3.28 2.95 32.77 80 73
5 11.27 8.89 10.22 1.00 31.37 NA NA
6 23.20 2.26 4.22 0.58 30.26 NA NA
User_Score User_Count Developer Rating
1 8 322 Nintendo E
2 NA
3 8.3 709 Nintendo E
4 8 192 Nintendo E
5 NA
6 NA
games %>% str()'data.frame': 16719 obs. of 16 variables:
$ Name : chr "Wii Sports" "Super Mario Bros." "Mario Kart Wii" "Wii Sports Resort" ...
$ Platform : chr "Wii" "NES" "Wii" "Wii" ...
$ Year_of_Release: chr "2006" "1985" "2008" "2009" ...
$ Genre : chr "Sports" "Platform" "Racing" "Sports" ...
$ Publisher : chr "Nintendo" "Nintendo" "Nintendo" "Nintendo" ...
$ NA_Sales : num 41.4 29.1 15.7 15.6 11.3 ...
$ EU_Sales : num 28.96 3.58 12.76 10.93 8.89 ...
$ JP_Sales : num 3.77 6.81 3.79 3.28 10.22 ...
$ Other_Sales : num 8.45 0.77 3.29 2.95 1 0.58 2.88 2.84 2.24 0.47 ...
$ Global_Sales : num 82.5 40.2 35.5 32.8 31.4 ...
$ Critic_Score : int 76 NA 82 80 NA NA 89 58 87 NA ...
$ Critic_Count : int 51 NA 73 73 NA NA 65 41 80 NA ...
$ User_Score : chr "8" "" "8.3" "8" ...
$ User_Count : int 322 NA 709 192 NA NA 431 129 594 NA ...
$ Developer : chr "Nintendo" "" "Nintendo" "Nintendo" ...
$ Rating : chr "E" "" "E" "E" ...
Note: check the output provided by str(). If User_Score and Year_of_Release are factors, run the following code to convert them to numeric vectors.
games$Year_of_Release <- as.numeric(as.character(games$Year_of_Release))Warning: NAs introduced by coercion
games$User_Score <- as.numeric(as.character(games$User_Score))Warning: NAs introduced by coercion
- The following code calculates the average game sales (in millions of units) in North America (NA), European Union (EU), and Japan (JP) for the games developed by Nintendo. Write the pipe version of the same code (i.e., use the same functions, but use the pipe operator to call each function).
colMeans(subset(games, Developer == "Nintendo")[, c(6,7,8)])Show solution
games %>%
subset(Developer == "Nintendo") %>%
'['(, c(6, 7, 8)) %>%
colMeans()NA_Sales EU_Sales JP_Sales
3.213056 2.324583 1.241806
- Subset the dataset so that the only games you see are those that belong to the “Shooter” genre. Print the data for the first five shooter games.
Show solution
games %>%
filter(Genre == "Shooter") %>%
slice(1:5) Name Platform Year_of_Release Genre Publisher
1 Duck Hunt NES 1984 Shooter Nintendo
2 Call of Duty: Modern Warfare 3 X360 2011 Shooter Activision
3 Call of Duty: Black Ops 3 PS4 2015 Shooter Activision
4 Call of Duty: Black Ops X360 2010 Shooter Activision
5 Call of Duty: Black Ops II PS3 2012 Shooter Activision
NA_Sales EU_Sales JP_Sales Other_Sales Global_Sales Critic_Score Critic_Count
1 26.93 0.63 0.28 0.47 28.31 NA NA
2 9.04 4.24 0.13 1.32 14.73 88 81
3 6.03 5.86 0.36 2.38 14.63 NA NA
4 9.70 3.68 0.11 1.13 14.61 87 89
5 4.99 5.73 0.65 2.42 13.79 83 21
User_Score User_Count Developer Rating
1 NA NA
2 3.4 8713 Infinity Ward, Sledgehammer Games M
3 NA NA
4 6.3 1454 Treyarch M
5 5.3 922 Treyarch M
- Subset the dataset so that the only variables you see are the name of the game, the platform on which the game runs, the year of release, and the genre. Print the data for the first five games.
Show solution
games %>%
select(Name, Platform, Year_of_Release, Genre) %>%
slice(1:5) Name Platform Year_of_Release Genre
1 Wii Sports Wii 2006 Sports
2 Super Mario Bros. NES 1985 Platform
3 Mario Kart Wii Wii 2008 Racing
4 Wii Sports Resort Wii 2009 Sports
5 Pokemon Red/Pokemon Blue GB 1996 Role-Playing
- Print the data for the top ten games in terms of the total number of units sold worldwide.
Show solution
games %>%
arrange(desc(Global_Sales)) %>%
slice(1:10) Name Platform Year_of_Release Genre Publisher
1 Wii Sports Wii 2006 Sports Nintendo
2 Super Mario Bros. NES 1985 Platform Nintendo
3 Mario Kart Wii Wii 2008 Racing Nintendo
4 Wii Sports Resort Wii 2009 Sports Nintendo
5 Pokemon Red/Pokemon Blue GB 1996 Role-Playing Nintendo
6 Tetris GB 1989 Puzzle Nintendo
7 New Super Mario Bros. DS 2006 Platform Nintendo
8 Wii Play Wii 2006 Misc Nintendo
9 New Super Mario Bros. Wii Wii 2009 Platform Nintendo
10 Duck Hunt NES 1984 Shooter Nintendo
NA_Sales EU_Sales JP_Sales Other_Sales Global_Sales Critic_Score
1 41.36 28.96 3.77 8.45 82.53 76
2 29.08 3.58 6.81 0.77 40.24 NA
3 15.68 12.76 3.79 3.29 35.52 82
4 15.61 10.93 3.28 2.95 32.77 80
5 11.27 8.89 10.22 1.00 31.37 NA
6 23.20 2.26 4.22 0.58 30.26 NA
7 11.28 9.14 6.50 2.88 29.80 89
8 13.96 9.18 2.93 2.84 28.92 58
9 14.44 6.94 4.70 2.24 28.32 87
10 26.93 0.63 0.28 0.47 28.31 NA
Critic_Count User_Score User_Count Developer Rating
1 51 8.0 322 Nintendo E
2 NA NA NA
3 73 8.3 709 Nintendo E
4 73 8.0 192 Nintendo E
5 NA NA NA
6 NA NA NA
7 65 8.5 431 Nintendo E
8 41 6.6 129 Nintendo E
9 80 8.4 594 Nintendo E
10 NA NA NA
- Subset the dataset so that the only games you see are those that are played on N64 (Nintendo 64) and that had more than two million units sold in North America (NA_Sales > 2). Print the Name, Genre, and NA_Sales number for these games.
Show solution
games %>%
filter(Platform == "N64", NA_Sales > 2) %>%
select(Name, Genre, NA_Sales) Name Genre NA_Sales
1 Super Mario 64 Platform 6.91
2 Mario Kart 64 Racing 5.55
3 GoldenEye 007 Shooter 5.80
4 The Legend of Zelda: Ocarina of Time Action 4.10
5 Super Smash Bros. Fighting 2.95
6 Pokemon Stadium Strategy 3.18
7 Donkey Kong 64 Platform 3.33
8 Diddy Kong Racing Racing 2.91
9 Star Fox 64 Shooter 2.78
10 Pokemon Snap Simulation 2.23
11 Star Wars Episode I Racer Racing 2.31
- Create a new variable that measures the total number of units sold in North America and the European Union combined. Sort the data in terms of this new variable (from most copies to fewest copies), and then print the data for the top ten most sold games in NA and EU. You can print a few variables of your choice instead of all of the variables.
Show solution
games %>%
mutate(NA_EU_Sales = NA_Sales + EU_Sales) %>%
arrange(desc(NA_EU_Sales)) %>%
slice(1:10) %>%
select(Name, Platform, Genre, NA_EU_Sales) Name Platform Genre NA_EU_Sales
1 Wii Sports Wii Sports 70.32
2 Super Mario Bros. NES Platform 32.66
3 Mario Kart Wii Wii Racing 28.44
4 Duck Hunt NES Shooter 27.56
5 Wii Sports Resort Wii Sports 26.54
6 Tetris GB Puzzle 25.46
7 Wii Play Wii Misc 23.14
8 New Super Mario Bros. Wii Wii Platform 21.38
9 New Super Mario Bros. DS Platform 20.42
10 Pokemon Red/Pokemon Blue GB Role-Playing 20.16
- Find information for the top 10 role-playing games in terms of user score. Note that some games have user score recorded as “tbd”, and you need to disregard these games.
Show solution
games %>%
filter(Genre == "Role-Playing", !is.na(User_Score)) %>%
arrange(desc(User_Score)) %>%
slice(1:10) %>%
select(Name, Platform, Year_of_Release, Genre, User_Score) Name Platform Year_of_Release Genre
1 Breath of Fire III PSP 2005 Role-Playing
2 Boktai: The Sun is in Your Hand GBA 2003 Role-Playing
3 Golden Sun: The Lost Age GBA 2002 Role-Playing
4 Paper Mario: The Thousand-Year Door GC 2004 Role-Playing
5 Skies of Arcadia DC 2000 Role-Playing
6 Golden Sun GBA 2001 Role-Playing
7 The Witcher 3: Wild Hunt PC 2015 Role-Playing
8 Deus Ex PC 2000 Role-Playing
9 Final Fantasy VII PS 1997 Role-Playing
10 The Witcher 3: Wild Hunt PS4 2015 Role-Playing
User_Score
1 9.7
2 9.6
3 9.5
4 9.4
5 9.4
6 9.3
7 9.3
8 9.3
9 9.2
10 9.2
- What is the most sold game globally for each of the game genres? You can print a few variables of your choice instead of all of the variables.
Show solution
games %>%
group_by(Genre) %>%
slice_max(order_by = Global_Sales, n = 1) %>%
ungroup() %>%
select(Name, Genre, Global_Sales)# A tibble: 13 × 3
Name Genre Global_Sales
<chr> <chr> <dbl>
1 "" "" 2.39
2 "Grand Theft Auto V" "Action" 21.0
3 "Super Mario Land 2: 6 Golden Coins" "Adventure" 11.2
4 "Super Smash Bros. Brawl" "Fighting" 12.8
5 "Wii Play" "Misc" 28.9
6 "Super Mario Bros." "Platform" 40.2
7 "Tetris" "Puzzle" 30.3
8 "Mario Kart Wii" "Racing" 35.5
9 "Pokemon Red/Pokemon Blue" "Role-Playing" 31.4
10 "Duck Hunt" "Shooter" 28.3
11 "Nintendogs" "Simulation" 24.7
12 "Wii Sports" "Sports" 82.5
13 "Pokemon Stadium" "Strategy" 5.45
- Which are the top 3 game genres with the highest global sales number, on average?
Show solution
games %>%
group_by(Genre) %>%
summarise(
global_mean = mean(Global_Sales),
.groups = "drop"
) %>%
slice_max(order_by = global_mean, n = 3)# A tibble: 3 × 2
Genre global_mean
<chr> <dbl>
1 "" 1.21
2 "Platform" 0.933
3 "Shooter" 0.796
- How many games are in each of the genres?
Show solution
games %>% count(Genre) Genre n
1 2
2 Action 3370
3 Adventure 1303
4 Fighting 849
5 Misc 1750
6 Platform 888
7 Puzzle 580
8 Racing 1249
9 Role-Playing 1500
10 Shooter 1323
11 Simulation 874
12 Sports 2348
13 Strategy 683
- What are the top 10 games with the largest difference (in absolute value) between critics score and user score? Note that you need to disregard games with NA values for either of the scores (hint: use filter), and you need to multiply the user score by 10 so that the two scores are compared on the same scale. You can print a few variables of your choice instead of all of the variables.
Show solution
games %>%
filter(!is.na(Critic_Score), !is.na(User_Score)) %>%
mutate(User_Score_10 = 10 * User_Score) %>%
mutate(diff = abs(Critic_Score - User_Score_10)) %>%
arrange(desc(diff)) %>%
slice(1:10) %>%
select(Name, Genre, Platform, Critic_Score, User_Score_10) Name Genre Platform Critic_Score User_Score_10
1 Thrillville: Off the Rails Strategy PSP 72 6
2 Shrek Swamp Kart Speedway Racing GBA 27 89
3 Company of Heroes 2 Strategy PC 80 21
4 Call of Duty: Modern Warfare 3 Shooter PS3 88 32
5 Call of Duty: Modern Warfare 3 Shooter X360 88 34
6 Call of Duty: Modern Warfare 3 Shooter PC 78 25
7 Thrillville: Off the Rails Strategy PS2 70 17
8 Call of Duty: Modern Warfare 3 Shooter Wii 70 18
9 Antz Extreme Racing Racing XB 35 87
10 Football Manager Live Sports PC 82 31
- Repeat 12, but disregard “obscure” games by limiting to games that have sold more than 3 (million) copies globally.
Show solution
games %>%
filter(!is.na(Critic_Score), !is.na(User_Score), Global_Sales >= 3) %>%
mutate(User_Score_10 = 10 * User_Score) %>%
mutate(diff = abs(Critic_Score - User_Score_10)) %>%
arrange(desc(diff)) %>%
slice(1:10) %>%
select(Name, Genre, Platform, Critic_Score, User_Score_10) Name Genre Platform Critic_Score
1 Call of Duty: Modern Warfare 3 Shooter PS3 88
2 Call of Duty: Modern Warfare 3 Shooter X360 88
3 Diablo III Role-Playing PC 88
4 Call of Duty: Ghosts Shooter X360 73
5 Call of Duty: Ghosts Shooter PS3 71
6 FIFA 14 Sports PS3 86
7 Call of Duty: Infinite Warfare Shooter PS4 77
8 FIFA 14 Sports X360 84
9 Call of Duty: Ghosts Shooter PS4 78
10 FIFA 16 Sports XOne 84
User_Score_10
1 32
2 34
3 40
4 26
5 26
6 43
7 34
8 42
9 37
10 44
- What are the top 10 platforms in terms of number of games released? How many games were released on each of those 10 platforms? What are the average critic scores of the games on the 10 platforms (ignoring games with missing scores)? What was the median release year for games on each of the 10 platforms (ignoring games with missing release year)?
Show solution
platforms <- games %>%
group_by(Platform) %>%
summarize(
number_of_games = n(),
average_critic_score = mean(Critic_Score, na.rm = TRUE),
median_release_year = median(Year_of_Release, na.rm = TRUE),
.groups = "drop"
) %>%
slice_max(order_by = number_of_games, n = 10)
platforms# A tibble: 10 × 4
Platform number_of_games average_critic_score median_release_year
<chr> <int> <dbl> <dbl>
1 PS2 2161 68.7 2005
2 DS 2152 63.8 2008
3 PS3 1331 70.4 2011
4 Wii 1320 62.8 2009
5 X360 1262 68.6 2010
6 PSP 1209 67.4 2009
7 PS 1197 71.5 1998
8 PC 974 75.9 2010
9 XB 824 69.9 2004
10 GBA 822 67.4 2003