The problem statement I am addressing is analyzing NFL attendance data. It is interesting because it provides valuable information about the impact of different factors on attendance, which can act as a guide for people who make decisions regarding marketing of the matches.
To address this problem, I will use NFL attendance data files, attendance.csv, standings.csv, and games.csv and these files include information like team names, team cities, season years and etc.
Team owners can use this analysis to optimize their marketing strategies, ticket pricing, and scheduling decisions.
library(tidyverse) #This package is being used for data manipulation
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
suppressMessages(library(tidyverse)) #This suppresses the messages from the tidyverse package
The attendance.csv has 8, the games.csv has 19, and standings.csv has 15 variables.
attendance_data <- read.csv("attendance.csv") #Reads attendance.csv and assigns it to a variable
str(attendance_data) #Gets the structure of attendance data
## 'data.frame': 10846 obs. of 8 variables:
## $ team : chr "Arizona" "Arizona" "Arizona" "Arizona" ...
## $ team_name : chr "Cardinals" "Cardinals" "Cardinals" "Cardinals" ...
## $ year : int 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
## $ total : int 893926 893926 893926 893926 893926 893926 893926 893926 893926 893926 ...
## $ home : int 387475 387475 387475 387475 387475 387475 387475 387475 387475 387475 ...
## $ away : int 506451 506451 506451 506451 506451 506451 506451 506451 506451 506451 ...
## $ week : int 1 2 3 4 5 6 7 8 9 10 ...
## $ weekly_attendance: int 77434 66009 NA 71801 66985 44296 38293 62981 35286 52244 ...
attendance_missing_values <- sum(is.na(attendance_data)) #Gets the total of missing values attendance data
standings_data <- read.csv("standings.csv") #Reads standings.csv and assigns it to a variable
str(standings_data) #Gets the structure of standings data
## 'data.frame': 638 obs. of 15 variables:
## $ team : chr "Miami" "Indianapolis" "New York" "Buffalo" ...
## $ team_name : chr "Dolphins" "Colts" "Jets" "Bills" ...
## $ year : int 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
## $ wins : int 11 10 9 8 5 13 12 9 7 4 ...
## $ loss : int 5 6 7 8 11 3 4 7 9 12 ...
## $ points_for : int 323 429 321 315 276 346 333 321 367 185 ...
## $ points_against : int 226 326 321 350 338 191 165 255 327 359 ...
## $ points_differential : int 97 103 0 -35 -62 155 168 66 40 -174 ...
## $ margin_of_victory : num 6.1 6.4 0 -2.2 -3.9 9.7 10.5 4.1 2.5 -10.9 ...
## $ strength_of_schedule: num 1 1.5 3.5 2.2 1.4 -1.3 -2.5 -0.2 -1.4 0.4 ...
## $ simple_rating : num 7.1 7.9 3.5 0 -2.5 8.3 8 3.9 1.1 -10.5 ...
## $ offensive_ranking : num 0 7.1 1.4 0.5 -2.7 1.5 0 0.6 3.2 -8.1 ...
## $ defensive_ranking : num 7.1 0.8 2.2 -0.5 0.2 6.8 8 3.3 -2.1 -2.4 ...
## $ playoffs : chr "Playoffs" "Playoffs" "No Playoffs" "No Playoffs" ...
## $ sb_winner : chr "No Superbowl" "No Superbowl" "No Superbowl" "No Superbowl" ...
standings_missing_values <- sum(is.na(standings_data)) #Gets the total of missing values in standings data
games_data <- read.csv("games.csv") #Reads games.csv and assigns it to a variable
str(games_data) #Gets the structure of games data
## 'data.frame': 5324 obs. of 19 variables:
## $ year : int 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
## $ week : chr "1" "1" "1" "1" ...
## $ home_team : chr "Minnesota Vikings" "Kansas City Chiefs" "Washington Redskins" "Atlanta Falcons" ...
## $ away_team : chr "Chicago Bears" "Indianapolis Colts" "Carolina Panthers" "San Francisco 49ers" ...
## $ winner : chr "Minnesota Vikings" "Indianapolis Colts" "Washington Redskins" "Atlanta Falcons" ...
## $ tie : chr NA NA NA NA ...
## $ day : chr "Sun" "Sun" "Sun" "Sun" ...
## $ date : chr "September 3" "September 3" "September 3" "September 3" ...
## $ time : chr "13:00:00" "13:00:00" "13:01:00" "13:02:00" ...
## $ pts_win : int 30 27 20 36 16 27 21 14 21 41 ...
## $ pts_loss : int 27 14 17 28 0 7 16 10 16 14 ...
## $ yds_win : int 374 386 396 359 336 398 296 187 395 425 ...
## $ turnovers_win : int 1 2 0 1 0 0 1 2 2 3 ...
## $ yds_loss : int 425 280 236 339 223 249 278 252 355 167 ...
## $ turnovers_loss: int 1 1 1 1 1 1 1 3 4 2 ...
## $ home_team_name: chr "Vikings" "Chiefs" "Redskins" "Falcons" ...
## $ home_team_city: chr "Minnesota" "Kansas City" "Washington" "Atlanta" ...
## $ away_team_name: chr "Bears" "Colts" "Panthers" "49ers" ...
## $ away_team_city: chr "Chicago" "Indianapolis" "Carolina" "San Francisco" ...
games_missing_values <- sum(is.na(games_data)) #Gets the total of missing values in games data
games_data$week <- as.integer(games_data$week) #week column changed from chr to int in games data
## Warning: NAs introduced by coercion
#remove missing values from attendance and games data, no missing values found in standings data
attendance_data <- na.omit(attendance_data)
games_data <- na.omit(games_data)
#Display of the top 10 observations of the cleaned data
head(attendance_data, 10)
## team team_name year total home away week weekly_attendance
## 1 Arizona Cardinals 2000 893926 387475 506451 1 77434
## 2 Arizona Cardinals 2000 893926 387475 506451 2 66009
## 4 Arizona Cardinals 2000 893926 387475 506451 4 71801
## 5 Arizona Cardinals 2000 893926 387475 506451 5 66985
## 6 Arizona Cardinals 2000 893926 387475 506451 6 44296
## 7 Arizona Cardinals 2000 893926 387475 506451 7 38293
## 8 Arizona Cardinals 2000 893926 387475 506451 8 62981
## 9 Arizona Cardinals 2000 893926 387475 506451 9 35286
## 10 Arizona Cardinals 2000 893926 387475 506451 10 52244
## 11 Arizona Cardinals 2000 893926 387475 506451 11 64223
head(standings_data, 10)
## team team_name year wins loss points_for points_against
## 1 Miami Dolphins 2000 11 5 323 226
## 2 Indianapolis Colts 2000 10 6 429 326
## 3 New York Jets 2000 9 7 321 321
## 4 Buffalo Bills 2000 8 8 315 350
## 5 New England Patriots 2000 5 11 276 338
## 6 Tennessee Titans 2000 13 3 346 191
## 7 Baltimore Ravens 2000 12 4 333 165
## 8 Pittsburgh Steelers 2000 9 7 321 255
## 9 Jacksonville Jaguars 2000 7 9 367 327
## 10 Cincinnati Bengals 2000 4 12 185 359
## points_differential margin_of_victory strength_of_schedule simple_rating
## 1 97 6.1 1.0 7.1
## 2 103 6.4 1.5 7.9
## 3 0 0.0 3.5 3.5
## 4 -35 -2.2 2.2 0.0
## 5 -62 -3.9 1.4 -2.5
## 6 155 9.7 -1.3 8.3
## 7 168 10.5 -2.5 8.0
## 8 66 4.1 -0.2 3.9
## 9 40 2.5 -1.4 1.1
## 10 -174 -10.9 0.4 -10.5
## offensive_ranking defensive_ranking playoffs sb_winner
## 1 0.0 7.1 Playoffs No Superbowl
## 2 7.1 0.8 Playoffs No Superbowl
## 3 1.4 2.2 No Playoffs No Superbowl
## 4 0.5 -0.5 No Playoffs No Superbowl
## 5 -2.7 0.2 No Playoffs No Superbowl
## 6 1.5 6.8 Playoffs No Superbowl
## 7 0.0 8.0 Playoffs Won Superbowl
## 8 0.6 3.3 No Playoffs No Superbowl
## 9 3.2 -2.1 No Playoffs No Superbowl
## 10 -8.1 -2.4 No Playoffs No Superbowl
head(games_data, 10)
## year week home_team away_team winner
## 650 2002 10 Pittsburgh Steelers Atlanta Falcons Pittsburgh Steelers
## 2273 2008 11 Cincinnati Bengals Philadelphia Eagles Philadelphia Eagles
## 3332 2012 10 San Francisco 49ers St. Louis Rams San Francisco 49ers
## 3621 2013 12 Green Bay Packers Minnesota Vikings Minnesota Vikings
## 3807 2014 6 Cincinnati Bengals Carolina Panthers Cincinnati Bengals
## 4362 2016 7 Arizona Cardinals Seattle Seahawks Seattle Seahawks
## 4365 2016 8 Cincinnati Bengals Washington Redskins Washington Redskins
## 4796 2018 1 Cleveland Browns Pittsburgh Steelers Pittsburgh Steelers
## 4808 2018 2 Green Bay Packers Minnesota Vikings Minnesota Vikings
## 5069 2019 1 Arizona Cardinals Detroit Lions Detroit Lions
## tie day date time pts_win pts_loss yds_win
## 650 Atlanta Falcons Sun November 10 13:01:00 34 34 645
## 2273 Cincinnati Bengals Sun November 16 13:03:00 13 13 391
## 3332 St. Louis Rams Sun November 11 16:25:00 24 24 341
## 3621 Green Bay Packers Sun November 24 13:02:00 26 26 447
## 3807 Carolina Panthers Sun October 12 13:03:00 37 37 513
## 4362 Arizona Cardinals Sun October 23 20:30:00 6 6 257
## 4365 Cincinnati Bengals Sun October 30 08:36:00 27 27 546
## 4796 Cleveland Browns Sun September 9 13:00:00 21 21 472
## 4808 Green Bay Packers Sun September 16 13:00:00 29 29 480
## 5069 Arizona Cardinals Sun September 8 16:25:00 27 27 477
## turnovers_win yds_loss turnovers_loss home_team_name home_team_city
## 650 3 447 2 Steelers Pittsburgh
## 2273 4 282 1 Bengals Cincinnati
## 3332 0 458 1 49ers San Francisco
## 3621 1 494 0 Packers Green Bay
## 3807 2 431 1 Bengals Cincinnati
## 4362 0 443 0 Cardinals Arizona
## 4365 1 415 2 Bengals Cincinnati
## 4796 6 327 1 Browns Cleveland
## 4808 1 351 0 Packers Green Bay
## 5069 2 387 1 Cardinals Arizona
## away_team_name away_team_city
## 650 Falcons Atlanta
## 2273 Eagles Philadelphia
## 3332 Rams St. Louis
## 3621 Vikings Minnesota
## 3807 Panthers Carolina
## 4362 Seahawks Seattle
## 4365 Redskins Washington
## 4796 Steelers Pittsburgh
## 4808 Vikings Minnesota
## 5069 Lions Detroit
attendance_structure <- str(attendance_data) #Gets the structure of attendance_data
## 'data.frame': 10208 obs. of 8 variables:
## $ team : chr "Arizona" "Arizona" "Arizona" "Arizona" ...
## $ team_name : chr "Cardinals" "Cardinals" "Cardinals" "Cardinals" ...
## $ year : int 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
## $ total : int 893926 893926 893926 893926 893926 893926 893926 893926 893926 893926 ...
## $ home : int 387475 387475 387475 387475 387475 387475 387475 387475 387475 387475 ...
## $ away : int 506451 506451 506451 506451 506451 506451 506451 506451 506451 506451 ...
## $ week : int 1 2 4 5 6 7 8 9 10 11 ...
## $ weekly_attendance: int 77434 66009 71801 66985 44296 38293 62981 35286 52244 64223 ...
## - attr(*, "na.action")= 'omit' Named int [1:638] 3 32 48 55 72 94 103 136 142 162 ...
## ..- attr(*, "names")= chr [1:638] "3" "32" "48" "55" ...
standings_structure <- str(standings_data) #Gets the structure of standings_data
## 'data.frame': 638 obs. of 15 variables:
## $ team : chr "Miami" "Indianapolis" "New York" "Buffalo" ...
## $ team_name : chr "Dolphins" "Colts" "Jets" "Bills" ...
## $ year : int 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
## $ wins : int 11 10 9 8 5 13 12 9 7 4 ...
## $ loss : int 5 6 7 8 11 3 4 7 9 12 ...
## $ points_for : int 323 429 321 315 276 346 333 321 367 185 ...
## $ points_against : int 226 326 321 350 338 191 165 255 327 359 ...
## $ points_differential : int 97 103 0 -35 -62 155 168 66 40 -174 ...
## $ margin_of_victory : num 6.1 6.4 0 -2.2 -3.9 9.7 10.5 4.1 2.5 -10.9 ...
## $ strength_of_schedule: num 1 1.5 3.5 2.2 1.4 -1.3 -2.5 -0.2 -1.4 0.4 ...
## $ simple_rating : num 7.1 7.9 3.5 0 -2.5 8.3 8 3.9 1.1 -10.5 ...
## $ offensive_ranking : num 0 7.1 1.4 0.5 -2.7 1.5 0 0.6 3.2 -8.1 ...
## $ defensive_ranking : num 7.1 0.8 2.2 -0.5 0.2 6.8 8 3.3 -2.1 -2.4 ...
## $ playoffs : chr "Playoffs" "Playoffs" "No Playoffs" "No Playoffs" ...
## $ sb_winner : chr "No Superbowl" "No Superbowl" "No Superbowl" "No Superbowl" ...
games_structure <- str(games_data) #Gets the structure of games_data
## 'data.frame': 10 obs. of 19 variables:
## $ year : int 2002 2008 2012 2013 2014 2016 2016 2018 2018 2019
## $ week : int 10 11 10 12 6 7 8 1 2 1
## $ home_team : chr "Pittsburgh Steelers" "Cincinnati Bengals" "San Francisco 49ers" "Green Bay Packers" ...
## $ away_team : chr "Atlanta Falcons" "Philadelphia Eagles" "St. Louis Rams" "Minnesota Vikings" ...
## $ winner : chr "Pittsburgh Steelers" "Philadelphia Eagles" "San Francisco 49ers" "Minnesota Vikings" ...
## $ tie : chr "Atlanta Falcons" "Cincinnati Bengals" "St. Louis Rams" "Green Bay Packers" ...
## $ day : chr "Sun" "Sun" "Sun" "Sun" ...
## $ date : chr "November 10" "November 16" "November 11" "November 24" ...
## $ time : chr "13:01:00" "13:03:00" "16:25:00" "13:02:00" ...
## $ pts_win : int 34 13 24 26 37 6 27 21 29 27
## $ pts_loss : int 34 13 24 26 37 6 27 21 29 27
## $ yds_win : int 645 391 341 447 513 257 546 472 480 477
## $ turnovers_win : int 3 4 0 1 2 0 1 6 1 2
## $ yds_loss : int 447 282 458 494 431 443 415 327 351 387
## $ turnovers_loss: int 2 1 1 0 1 0 2 1 0 1
## $ home_team_name: chr "Steelers" "Bengals" "49ers" "Packers" ...
## $ home_team_city: chr "Pittsburgh" "Cincinnati" "San Francisco" "Green Bay" ...
## $ away_team_name: chr "Falcons" "Eagles" "Rams" "Vikings" ...
## $ away_team_city: chr "Atlanta" "Philadelphia" "St. Louis" "Minnesota" ...
## - attr(*, "na.action")= 'omit' Named int [1:5314] 1 2 3 4 5 6 7 8 9 10 ...
## ..- attr(*, "names")= chr [1:5314] "1" "2" "3" "4" ...
#Summary of each data set
summary_attendance_data <- summary(attendance_data)
summary_standings_data <- summary(standings_data)
summary_games_data <- summary(games_data)
I cleaned up the three data sets by changing variables with numbers
into integers. Specifically, in the games_data dataset, I
converted the week column from character (chr) to integers
(int). This transformation was done to ensure that the week
column is stored as numeric data rather than text, which can be more
useful for analysis and calculations. All three data sets had
NA values removed. Here are the structures and the summaries of
the cleaned data:
Attendance Variable: Length:10208 , Class :character , Mode :character , NA, NA, NA, Length:10208 , Class :character , Mode :character , NA, NA, NA, Min. :2000 , 1st Qu.:2005 , Median :2010 , Mean :2010 , 3rd Qu.:2015 , Max. :2019 , Min. : 760644 , 1st Qu.:1040509 , Median :1081090 , Mean :1080910 , 3rd Qu.:1123230 , Max. :1322087 , Min. :202687 , 1st Qu.:504360 , Median :543185 , Mean :540455 , 3rd Qu.:578342 , Max. :741775 , Min. :450295 , 1st Qu.:524974 , Median :541757 , Mean :540455 , 3rd Qu.:557741 , Max. :601655 , Min. : 1.000 , 1st Qu.: 5.000 , Median : 9.000 , Mean : 9.103 , 3rd Qu.:13.000 , Max. :17.000 , Min. : 23127 , 1st Qu.: 63246 , Median : 68334 , Mean : 67557 , 3rd Qu.: 72545 , Max. :105121
Standings Variable: Length:638 , Class :character , Mode :character , NA, NA, NA, Length:638 , Class :character , Mode :character , NA, NA, NA, Min. :2000 , 1st Qu.:2005 , Median :2010 , Mean :2010 , 3rd Qu.:2015 , Max. :2019 , Min. : 0.000 , 1st Qu.: 6.000 , Median : 8.000 , Mean : 7.984 , 3rd Qu.:10.000 , Max. :16.000 , Min. : 0.000 , 1st Qu.: 6.000 , Median : 8.000 , Mean : 7.984 , 3rd Qu.:10.000 , Max. :16.000 , Min. :161.0 , 1st Qu.:299.0 , Median :348.0 , Mean :350.3 , 3rd Qu.:396.0 , Max. :606.0 , Min. :165.0 , 1st Qu.:310.0 , Median :347.0 , Mean :350.3 , 3rd Qu.:391.5 , Max. :517.0 , Min. :-261.00 , 1st Qu.: -75.00 , Median : 1.50 , Mean : 0.00 , 3rd Qu.: 72.75 , Max. : 315.00 , Min. :-16.300000 , 1st Qu.: -4.700000 , Median : 0.100000 , Mean : -0.001881 , 3rd Qu.: 4.575000 , Max. : 19.700000 , Min. :-4.600000 , 1st Qu.:-1.100000 , Median : 0.000000 , Mean : 0.001097 , 3rd Qu.: 1.200000 , Max. : 4.300000 , Min. :-17.400 , 1st Qu.: -4.475 , Median : 0.000 , Mean : 0.000 , 3rd Qu.: 4.500 , Max. : 20.100 , Min. :-11.700000 , 1st Qu.: -3.175000 , Median : 0.000000 , Mean : -0.000157 , 3rd Qu.: 2.700000 , Max. : 15.900000 , Min. :-9.800000 , 1st Qu.:-2.400000 , Median : 0.100000 , Mean :-0.001097 , 3rd Qu.: 2.500000 , Max. : 9.800000 , Length:638 , Class :character , Mode :character , NA, NA, NA, Length:638 , Class :character , Mode :character , NA, NA, NA
Games Variable: Min. :2002 , 1st Qu.:2012 , Median :2015 , Mean :2014 , 3rd Qu.:2018 , Max. :2019 , Min. : 1.0 , 1st Qu.: 3.0 , Median : 7.5 , Mean : 6.8 , 3rd Qu.:10.0 , Max. :12.0 , Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Min. : 6.00 , 1st Qu.:21.75 , Median :26.50 , Mean :24.40 , 3rd Qu.:28.50 , Max. :37.00 , Min. : 6.00 , 1st Qu.:21.75 , Median :26.50 , Mean :24.40 , 3rd Qu.:28.50 , Max. :37.00 , Min. :257.0 , 1st Qu.:405.0 , Median :474.5 , Mean :456.9 , 3rd Qu.:504.8 , Max. :645.0 , Min. :0.00 , 1st Qu.:1.00 , Median :1.50 , Mean :2.00 , 3rd Qu.:2.75 , Max. :6.00 , Min. :282.0 , 1st Qu.:360.0 , Median :423.0 , Mean :403.5 , 3rd Qu.:446.0 , Max. :494.0 , Min. :0.00 , 1st Qu.:0.25 , Median :1.00 , Mean :0.90 , 3rd Qu.:1.00 , Max. :2.00 , Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA, Length:10 , Class :character , Mode :character , NA, NA, NA
#sums up the amount of people attended for all teams
total_attendance_by_team <- attendance_data %>%
group_by(team) %>%
summarise(total_attendance = sum(total))
ggplot(total_attendance_by_team, aes(x = reorder(team, -total_attendance), y = total_attendance)) +
geom_bar(stat = "identity", fill = "blue") + # Create a bar plot with the total attendance
labs(
title = "Total Attendance by Team", #Set the title
x = "Team", #Set x axis title
y = "Total Attendance" #Set y axis title
) + coord_flip() #Flips the coordinates to make a horizontal bar chart
# Calculate the number of games played by each team
games_count_by_team <- games_data %>%
group_by(home_team) %>%
summarise(num_games = n())
# Create a bar chart for games data
ggplot(games_count_by_team, aes(x = reorder(home_team, -num_games), y = num_games)) +
geom_bar(stat = "identity", fill = "green") +
labs(
title = "Number of Games Played by Team",
x = "Team",
y = "Number of Games"
) + coord_flip()
# Calculate the total wins for each team
total_wins_by_team <- standings_data %>%
group_by(team) %>%
summarise(total_wins = sum(wins))
# Create a bar chart for standings data (total wins)
ggplot(total_wins_by_team, aes(x = reorder(team, -total_wins), y = total_wins)) +
geom_bar(stat = "identity", fill = "orange") +
labs(
title = "Total Wins by Team",
x = "Team",
y = "Total Wins"
) + coord_flip()
I made three bar charts to showcase the different data each team has. I kept the data separate for my analysis because I could not figure out how to merge them properly without running into errors. So merging the data sets is something that I do not know how to do right now.