# --------------------- # # Mark the plays with how many points are scored at the end of a drive # # Currently, the data has no way to determine how many points are # scored at the end of a drive. # # This R Script will read in the SORTED .csv file, check to see what the # result of a drive is, and then mark the whole drive with the value # of how many points a drive resulted in. # # --------------------- # Libraries to Use library(tidyverse) # Set the working directory to match where the files are pulled from setwd("C:/Users/Ryant/Downloads/Capstone/Cleaned Data Files") # Read in the data Plays <- read_csv("pbpDriveEnd2021-2024.csv") # Flip the order to make running the loop to add variable easier Points <- Plays %>% arrange(desc(GameId), desc(PlayID)) # Run a loop to add point value PointsScored <- 0 # Initialize the variable Points$DrivePoints <- NA_integer_ # Create the new column # Set aside the rows that aren't a play NonPlays <- Points %>% filter(is.na(OffenseTeam) | is.na(DefenseTeam) | ( is.na(PreviousPlayID) & is.na(NextPlayID) )) # Grab only the rows of actual plays OnlyPlays <- Points %>% filter(!is.na(OffenseTeam) & !is.na(DefenseTeam) & ( !is.na(PreviousPlayID) | !is.na(NextPlayID) )) # Loop through rows and assign the value for (i in 1:nrow(OnlyPlays)) { if (OnlyPlays$DriveEnd[i] == TRUE) { # End of Drive # Field Goal Successful if (OnlyPlays$PlayTypeUpdate[i] == "FIELD GOAL" && str_detect(OnlyPlays$Description[i], "IS GOOD")) { PointsScored <- 3 # Assign value of points scored } # Following Touchdown, going for 1 else if (OnlyPlays$PlayTypeUpdate[i] == "EXTRA POINT") { # Got the extra point if (str_detect(OnlyPlays$Description[i], "IS GOOD")) { PointsScored <- 7 } # Failed the extra point else { PointsScored <- 6 } } # Following Touchdown, going for 2 else if (OnlyPlays$PlayTypeUpdate[i] == "TWO-POINT CONVERSION") { # Got the extra points if (str_detect(OnlyPlays$Description[i], "SUCCEEDS")) { PointsScored <- 8 } # Failed the extra points else { PointsScored <- 6 } } # The defense scored a Safety else if (str_detect(OnlyPlays$Description[i], "SAFETY")) { PointsScored <- -2 } # Punt / End of Game or Half else if (OnlyPlays$PlayTypeUpdate[i] == "PUNT" || (OnlyPlays$GameId[i] != OnlyPlays$GameId[i-1] && i > 1) || i == 1 || (OnlyPlays$Quarter[i] == 2 && OnlyPlays$Quarter[i-1] == 3)) { PointsScored <- 0 } # Turnover else { PointsScored <- PointsScored * -1 } } OnlyPlays$DrivePoints[i] <- PointsScored # Assign points scored to dataset } # Get all the data back in order PointsFinal <- rbind(OnlyPlays, NonPlays) %>% arrange(GameId, PlayID) %>% select(1:11, 46, 12:45) # Shift over the new variable # Save the CSV write_csv(PointsFinal, "pbpDrivePoints2021-2024.csv") print("Done!")