Let's say I have the following historical league results:
Season <- c(1,1,2,2,3,3,4,4,5,5)
Team <- c("Diverpool","Deverton","Diverpool","Deverton","Diverpool","Deverton","Diverpool","Deverton","Diverpool","Deverton")
End.Rank <- c(8,17,4,15,3,6,4,16,3,17)
PLRank <- cbind(Season,Team,End.Rank)
I want to (efficiently) create a variable in one year for each team based on two criteria:
- lag
End.Rankon Season(i.e. t-1 s Seasonas a temporary variable) - separately on command (Deverton lagged behind
End.RankDiverpool lagged End.Rank)
Essentially, I would like the result to be as follows:
l.End.Rank <- c(NA,NA,8,17,4,15,3,6,4,16)
Tried lag()and lost while trying to do this in a loop for()at the moment.