Read the data from all 6 of the CSV files into an R data frame. Add 3 unique rows at the top of the R data frame. These rows must be different for each student. Use appropriate values to avoid outliers.
Add a “Team_Your_First_Name” column to the data frame and populate it with the correct team for each player.
We need to set first our directory and then read all of the files inside that directory that are csv.
setwd("C:/Users/edwin/OneDrive/Escritorio/Personal files/Courses/Semester 2/Analytics Programming/Assignment/Assignment 2")
files <- list.files(pattern = "\\.csv$")
print(files)
[1] "BostonBruinsScoring-1960.csv" "ChicagoBlackHawksScoring-1960.csv" "DetroitRedWingsScoring-1960.csv"
[4] "montrealCanadiensScoring-1960.csv" "NewYorkRangersScoring-1960.csv" "TorontoMapleLeafsScoring-1960.csv"
Based on that, we can read each of our files and put them in one unique dataframe called “df”. Also we will add a column to identify the team that belongs to.
df<-data.frame()
for (file in files) {
##read
df_each <- read.csv(file)
##file name read
name_parts <- strsplit(file, "-")[[1]]
#getting team name
team_name<-name_parts[1]
df_each$Team_Edwin=team_name
#getting year
name_year <- strsplit(file, "-")[[1]]
year_split<-name_year[2]
year_split2 <- strsplit(year_split, ".csv")
year<-year_split2[[1]]
df_each$Year<-year
##appending
df<-bind_rows(df,df_each)
}
head(df)
Let’s observe the basic statistics from our created dataframe.
summary(df)
Player Age Pos GP G A
Length:153 Min. :18.00 Length:153 Min. : 1.0 Min. : 0.000 Min. : 0.00
Class :character 1st Qu.:23.00 Class :character 1st Qu.:22.0 1st Qu.: 1.000 1st Qu.: 2.00
Mode :character Median :25.00 Mode :character Median :54.0 Median : 5.000 Median :10.00
Mean :25.84 Mean :44.5 Mean : 8.351 Mean :13.91
3rd Qu.:28.00 3rd Qu.:67.0 3rd Qu.:14.000 3rd Qu.:21.00
Max. :36.00 Max. :70.0 Max. :50.000 Max. :58.00
NA's :2 NA's :2 NA's :2 NA's :2
X... PIM EV PP SH GW S
Min. :-31.0000 Min. : 0.00 Min. : 0.000 Min. : 0.00 Min. :0.000 Min. : 0.000 Min. : 0.00
1st Qu.: -7.0000 1st Qu.: 4.00 1st Qu.: 1.000 1st Qu.: 0.00 1st Qu.:0.000 1st Qu.: 0.000 1st Qu.: 23.50
Median : -1.0000 Median : 18.00 Median : 4.000 Median : 0.00 Median :0.000 Median : 0.000 Median : 74.00
Mean : -0.3245 Mean : 31.11 Mean : 6.596 Mean : 1.51 Mean :0.245 Mean : 1.132 Mean : 90.13
3rd Qu.: 3.0000 3rd Qu.: 48.00 3rd Qu.:11.000 3rd Qu.: 2.00 3rd Qu.:0.000 3rd Qu.: 2.000 3rd Qu.:132.50
Max. : 31.0000 Max. :169.00 Max. :41.000 Max. :16.00 Max. :4.000 Max. :12.000 Max. :357.00
NA's :2 NA's :2 NA's :2 NA's :2 NA's :2 NA's :2 NA's :3
Team_Edwin Year
Length:153 Length:153
Class :character Class :character
Mode :character Mode :character
Now we are ready to create our 3 new players and add it too our dataframe.
Based on the available data, I am going to be creating 3 new players, considering that are not outliers.
players_df <- data.frame(
Player = c("Edwin Santos", "Javier Masias", "Lucia Rosas"),
Age = c(25, 27, 26), # Within 23 to 28
Pos = c("RW", "C", "LW"),
GP = c(30, 60, 50), # Between 22 and 67
G = c(5, 10, 7), # Between 1 and 14
A = c(5, 15, 10), # Between 2 and 21
X... = c(-3 , 1 , -2),
PIM = c(10, 40, 30), # Between 4 and 48
EV = c(3, 9, 5), # Between 1 and 11
PP = c(1, 1, 2), # Between 0 and 2
SH = c(0, 0, 0),
GW = c(1, 2, 1), # Between 0 and 2
S = c(50, 100, 75), # Between 23.5 and 132.5
Team_Edwin = c("montrealCanadiensScoring","montrealCanadiensScoring","montrealCanadiensScoring"),
Year = c("1960","1960","1960")
)
print(players_df)
Now I am going to be adding to our main dataframe.
df<-bind_rows(players_df,df)
head(df)
We are ready to continue with the next steps.
Using R, add a “Pts_Your_Last_Name” (Points) column for each player by adding the G (Goals) and A (assists) columns for each player.
This is something very straightforward. Just adding both goals and assists give us the column required.
df$PTS_Santos=df$G+df$A
head(df)
Add a column for shooting percentage for each player and name the column “ShPerc_Your_First_Name”. Hint: do math involving shots and goals to get the shooting percentage.
The shooting percentage is the division between goals and shots, which can be calculated by following this equation
Equation of shooting percentage: \(ShPerc_{Santos} = G/S\)
df$ShPerc_Santos=df$G/df$S
head(df)
Create a bar graph displaying the total goals by team that year.
df=na.omit(df)
sum_goals_per_team <- df %>%
group_by(Team_Edwin) %>%
summarise(sumgoals = sum(G))
sum_goals_per_team$Team_Edwin <- reorder(sum_goals_per_team$Team_Edwin, -sum_goals_per_team$sumgoals)
ggplot(sum_goals_per_team, aes(x = Team_Edwin, y = sumgoals)) +
geom_col() +
geom_text(aes(label = sumgoals), vjust = 1.5, angle = 0, position = position_dodge(width = 0.9),color = "white")+
labs(
x = "Team Edwin",
y = "Goals",
caption = "Source: Your Source Here") + # Add a caption if needed
theme(axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_blank(), # Removes major grid lines
panel.grid.minor = element_blank(), # Removes minor grid lines
panel.background = element_blank()) # Removes background color/panel
Create a bar graph displaying the average age of each team.
average_age_per_team <- df %>%
group_by(Team_Edwin) %>%
summarise(AverageAge = round( mean(Age, na.rm = TRUE),2))
average_age_per_team$Team_Edwin <- reorder(average_age_per_team$Team_Edwin, -average_age_per_team$AverageAge)
ggplot(average_age_per_team, aes(x = Team_Edwin, y = AverageAge)) +
geom_col() +
geom_text(aes(label = AverageAge), vjust = 1.5, angle = 0, position = position_dodge(width = 0.9),color = "white")+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Average Age per Team",
x = "Team",
y = "Average Age")
Create a bar graph displaying the number of players at each position in the league
count_players_pos <- df %>%
group_by(Pos) %>%
summarise(Count_Players = n(), .groups = 'drop')
count_players_pos$Pos <- reorder(count_players_pos$Pos, -count_players_pos$Count_Players)
ggplot(count_players_pos, aes(x = Pos, y = Count_Players)) +
geom_col() +
geom_text(aes(label = Count_Players), vjust = 1.5, angle = 0, position = position_dodge(width = 0.9),color = "white")+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Count Players per Position",
x = "Position",
y = "Count of Players")