I have a data base with plays of a football game in order of each play. I want to make a for loop that checks if the play before a field goal (one row up) was a timeout by the other team. I tried using [i - 1] below but not sure if that works as I get an error.
for (i in 1:nrow(example)) {
if (example$field_goal_attempt[i] == 1 & example$timeout[i-1] == 1 & example$timeout_team[i-1] !=
example$posteam[i-1]) {
example$iced[i] == 1
} else {
example$iced[i] == 0
}
}
example <- data.frame(game_id = c(10,10,10,10,10,13,13,13,13,13),
field_goal_attempted = c(0,0,0,1,0,0,0,0,0,0), time_out =
c(0,0,1,0,0,0,0,0,0,0), posteam =
c("PIT","PIT","PIT","PIT","PIT","ATL","ATL","WAS","WAS","WAS"), timeout_team
= c(NA, NA, "TEN",NA,NA,NA,NA,NA,NA,NA))
Show in New WindowClear OutputExpand/Collapse Output Error in if (example$field_goal_attempt[i] == 1 & example$timeout[i - : argument is of length zero