2

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.

The picture is the example of the dataaframe. starting from 4 I want 14 DFs with the sums of each week aggregated Sigma4!

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

d3hero23
  • 380
  • 1
  • 12
  • Perhaps should `{for (i in 1:17)` really be `for (i in 1:17) {`? – r2evans Sep 24 '19 at 20:30
  • No that doesnt work. It then says there is nothing to aggregate when doing that – d3hero23 Sep 24 '19 at 21:00
  • d3hero23, please make this question *reproducible*. This includes sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Sep 24 '19 at 22:21
  • An abbreviated version would of the data frame would bePlayerID = 113345 x length of A, A = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) Fantasy points= (11,15,16, .etc) What I am trying to with the function is to sum all of the columns from week x up until that point. Using the example Week 4 would be the sum of 11+ 15 + 16 and that would be stored into df4. The same for week 5 so 11+15+16+y stored to df 5. – d3hero23 Sep 24 '19 at 23:03
  • Most data and code are hard to read in comments, so they do much better in the question itself. (Additionally, comments can easily be skipped by readers or hidden by SO if there are too many comments.) Your sample data is a start, but it is typically much better to include data that is specifically usable in your function and on our R console. – r2evans Sep 24 '19 at 23:15
  • @r2evans I added in a snapshot of the csv abbreviated – d3hero23 Sep 25 '19 at 18:25
  • Thanks for adding that, though it's not something we can easily copy and try in our own console. Images of data: cannot be copied or searched (SEO); break screen-readers; and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557/3358272 (and https://xkcd.com/2116/). Please just include the code or data (e.g., `dput(head(x))` or `data.frame(...)`) directly. – r2evans Sep 25 '19 at 18:33
  • @r2evans Thanks for the tips. I have included the code. I always appreciate tips as I am a self taught coder. – d3hero23 Sep 26 '19 at 13:36
  • d3hero23, this is getting better, but I feel that making it "truly easy" for somebody to play with your data is important. As it stands, while it will take less work to copy what you've provided, tibbles are particularly bad for "just copying" (and using `read.table`). Really, the [first link](https://stackoverflow.com/questions/5963269) in my second comment (above) suggests `dput`, and my previous comment suggests `dput` (or `data.frame`) not console output. In addition to being "not easy" to just copy-and-use, there is either ambiguity or truncation in console output (it is usually "lossy"). – r2evans Sep 26 '19 at 18:23
  • (The use of `dput` can easily be subset so that you aren't dumping too many rows or more columns than needed. For instance, `dput(x[2:5,1:12])` is very well contained. Keeping with the "mwe" mindset, provide enough columns and rows to show variability/groups, and if possible little or nothing more.) – r2evans Sep 26 '19 at 18:24

0 Answers0