library(readr)
shots<- read_csv("C:/Users/cjezr/Downloads/shots_data.csv")
## Parsed with column specification:
## cols(
## team = col_character(),
## x = col_double(),
## y = col_double(),
## fgmade = col_double()
## )
#create a new column in the data frame called "zone" that describes where the shot was taken. either 2pt, nc3, or c3
zone=numeric()
i=1
while(i<=length(shots$team))
{
dist=sqrt(shots$x[i]**2+shots$y[i]**2) #dist is the distance from hoop to shot
if(abs(shots$x[i])>=22 & shots$y[i]<=7.8) #critiria for corner three
{
zone[i]="c3"
}
else if(dist>=23.75 & shots$y[i] > 7.8) #criteria for non corner three
{
zone[i]="nc3"
}
else if(dist<23.75 & shots$y[i]>7.8) #criteria for non corner 2
{
zone[i]="2pt"
}
else if(shots$y[i]<=7.8 & abs(shots$x[i])<22) #criteria for corner 2
{
zone[i]="2pt"
}
else
{
zone[i]="error" #if none of the previous then error
}
i=i+1
}
shots$zone=zone
#For Team A, what percentage of their shots were attempted in the 2PT zone?
library(tidyverse)
## -- Attaching packages ------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0 v dplyr 0.8.5
## v tibble 3.0.1 v stringr 1.4.0
## v tidyr 1.0.2 v forcats 0.5.0
## v purrr 0.3.3
## -- Conflicts ---------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
teamA=filter(shots,team=="Team A") #teamA is a dataframe of team A's shots
teamB=filter(shots,team=="Team B") #teamB is a dataframe of team B's shots
sum(teamA$zone=="2pt")/length(teamA$zone)#sum of 2pt shots divided by the total
## [1] 0.6071429
#For Team A, what percentage of their shots were attempted in the NC3 zone?
sum(teamA$zone=="nc3")/length(teamA$zone)#sum of nc3 shots divided by the total
## [1] 0.3214286
#For Team A, what percentage of their shots were attempted in the C3 zone?
sum(teamA$zone=="c3")/length(teamA$zone)#sum of c3 shots divided by the total
## [1] 0.07142857
#For Team A, what was the eFG in the 2PT zone? Rounded to 3 decimal places?
#since they are all 2pt it is just the fg percentage
sum(teamA$zone=="2pt" & teamA$fgmade==1)/sum(teamA$zone=="2pt")
## [1] 0.4
#For Team A, what was the eFG in the NC3 zone? Rounded to 3 decimal places?
#must adjust the equation for 3s by multiplying the threes made by 1.5
(1.5*sum(teamA$zone=="nc3" & teamA$fgmade==1))/sum(teamA$zone=="nc3")
## [1] 0.4833333
#For Team A, what was the eFG in the C3 zone? Rounded to 3 decimal places?
#must adjust the equation for 3s by multiplying the threes made by 1.5
(1.5*sum(teamA$zone=="c3" & teamA$fgmade==1))/sum(teamA$zone=="c3")
## [1] 0.6
#For Team B, what percentage of their shots were attempted in the 2PT zone?
sum(teamB$zone=="2pt")/length(teamB$zone)#sum of 2pt shots divided by the total
## [1] 0.5821429
#For Team B, what percentage of their shots were attempted in the NC3 zone?
sum(teamB$zone=="nc3")/length(teamB$zone)#sum of nc3 shots divided by the total
## [1] 0.3428571
#For Team B, what percentage of their shots were attempted in the C3 zone?
sum(teamB$zone=="c3")/length(teamB$zone)#sum of c3 shots divided by the total
## [1] 0.075
#For Team B, what was the eFG in the 2PT zone? Rounded to 3 decimal places?
#since they are all 2pt it is just the fg percentage
sum(teamB$zone=="2pt" & teamB$fgmade==1)/sum(teamB$zone=="2pt")
## [1] 0.4601227
#For Team B, what was the eFG in the NC3 zone? Rounded to 3 decimal places?
#must adjust the equation for 3s by multiplying the threes made by 1.5
(1.5*sum(teamB$zone=="nc3" & teamB$fgmade==1))/sum(teamB$zone=="nc3")
## [1] 0.546875
#For Team B, what was the eFG in the C3 zone? Rounded to 3 decimal places?
#must adjust the equation for 3s by multiplying the threes made by 1.5
(1.5*sum(teamB$zone=="c3" & teamB$fgmade==1))/sum(teamB$zone=="c3")
## [1] 0.3571429