Вопрос

I am trying to produce stats for cumulative goals by season by a particular soccer player. I have used the cut function to obtain the season from the game dates. I have data which corresponds to this dataframe

df.raw <-
    data.frame(Game = 1:20,
            Goals=c(1,0,0,2,1,0,3,2,0,0,0,1,0,4,1,2,0,0,0,3),     
               season = gl(4,5,labels = c("2001", "2002","2003", "2004")))

In real life, the number of games per season may not be constant

I want to end up with data that looks like this

df.seasoned <-
    data.frame(Game = 1:20,seasonGame= rep(1:5),
            Goals=c(1,0,0,2,1,0,3,2,0,0,0,1,0,4,1,2,0,0,0,3),
            cumGoals = c(1,1,1,3,4,0,3,5,5,5,0,1,1,5,6,2,2,2,2,5),     
               season = gl(4,5,labels = c("2001", "2002","2003", "2004")))

With the goals cumulatively summed within year and a game number for the season

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top