I am trying to create multiple DFs from a function with each DF being the aggregate of up until varying row values. For your reference I am using fantasy football data. So right now I have each players stats for every week. I want to create a data frame for each week and their cumulative stats until that week.
Here is my function that I currently am using which only creates one list of aggregating the week 17 values.
sumuptopoint <- function(dfx,i) { listofdfs <- list()
dfy <- dfx[, !sapply(dfx, is.character)]
{for (i in 1:17)
dft <- dfy[dfy$Week < i,]
y <<- as.data.frame(aggregate(dft, list("PlayerID" = dft$PlayerID), sum))
listofdfs[[i]] <- y}
return(listofdfs)}
I expect 17 lists of aggregated data but am only get 1 list where 17 weeks prior to 17 are aggregated
Here is the df:
Team ByeWeek Rank.all PlayerID Name Position Week Opponent PassingCompletio~ PassingAttempts.~ PassingCompletio~ PassingYards.all PassingTouchdow~ PassingIntercep~ PassingRating.a~
<chr> <int> <int> <int> <chr> <chr> <dbl> <chr> <int> <int> <dbl> <int> <int> <int> <dbl>
1 ARI 12 201 19763 Josh ~ QB 8.00 SF 23 40 57.5 252 2 1 82.5
2 ARI 12 319 19763 Josh ~ QB 11.0 OAK 9 20 45.0 136 3 2 67.9
3 ARI 12 372 19763 Josh ~ QB 4.00 SEA 15 27 55.6 180 1 0 88.5
4 ARI 12 392 11527 Sam B~ QB 3.00 CHI 13 19 68.4 157 2 2 89.0
5 ARI 12 407 19763 Josh ~ QB 5.00 SF 10 25 40.0 170 1 0 77.1
6 ARI 12 411 19763 Josh ~ QB 10.0 KC 22 39 56.4 208 1 2 58.5