tatiwawa
tatiwawa

Reputation: 35

Combining a loop with stacking dataframes created by a function

I'm doing some analysis with the BaseballR package and want to be able to combine dataframes by using a loop.

For example, the following code using the standings_on_date_bref function gives me a table of division standings for the specified day (plus manually adding a column for the date of those standings):

library("baseballr")
library("dplyr")
standings_on_date_bref(date = "04-28-2021", division = "NL West") %>%
mutate(date = "04-28-2021")
Tm W-L% date
SFG 0.640 04-28-2021
LAD 0.640 04-28-2021
SDP 0.538 04-28-2021
ARI 0.500 04-28-2021
COL 0.375 04-28-2021

However, I'm interested in getting the standings for a whole range of days (which would end up being a dataframe with rows = 5 teams * x number of days) for example for 04-28-2021 to 04-29-2021, I'm hoping it would look something like this:

Tm W-L% date
SFG 0.640 04-28-2021
LAD 0.640 04-28-2021
SDP 0.538 04-28-2021
ARI 0.500 04-28-2021
COL 0.375 04-28-2021
SFG 0.640 04-29-2021
LAD 0.615 04-29-2021
SDP 0.538 04-29-2021
ARI 0.520 04-29-2021
COL 0.360 04-29-2021

I have tried to do so by implementing some sort of loop. This is what I've come up with so far, but in the end it just gives me the standings for the end date.

start <- as.Date("04-01-21",format="%m-%d-%y")
end   <- as.Date("04-03-21",format="%m-%d-%y")

theDate <- start

while (theDate <= end)
{
  all_standings <- standings_on_date_bref(date = theDate, division = "NL West") %>%
    mutate(date = theDate)
  theDate <- theDate + 1    
}

Upvotes: 2

Views: 52

Answers (1)

Sinh Nguyen
Sinh Nguyen

Reputation: 4487

You can try purrr which would do it quite nicely with map_dfr function

library(baseballr)
library(dplyr)
library(purrr)

date_seq <- seq(as.Date("04-01-21",format="%m-%d-%y"), 
                as.Date("04-03-21",format="%m-%d-%y"), by = "1 day")

map_dfr(.x = date_seq,
        .f = function(x) {
          standings_on_date_bref(date = x, division = "NL West") %>%
            mutate(date = x)
        })
#> # A tibble: 15 x 9
#>    Tm        W     L `W-L%` GB       RS    RA `pythW-L%` date      
#>    <chr> <int> <int>  <dbl> <chr> <int> <int>      <dbl> <date>    
#>  1 SDP       1     0  1     --        8     7      0.561 2021-04-01
#>  2 COL       1     0  1     --        8     5      0.703 2021-04-01
#>  3 ARI       0     1  0     1.0       7     8      0.439 2021-04-01
#>  4 SFG       0     1  0     1.0       7     8      0.439 2021-04-01
#>  5 LAD       0     1  0     1.0       5     8      0.297 2021-04-01
#>  6 SDP       2     0  1     --       12     9      0.629 2021-04-02
#>  7 COL       1     1  0.5   1.0      14    16      0.439 2021-04-02
#>  8 SFG       1     1  0.5   1.0      13    11      0.576 2021-04-02
#>  9 LAD       1     1  0.5   1.0      16    14      0.561 2021-04-02
#> 10 ARI       0     2  0     2.0       9    12      0.371 2021-04-02
#> 11 SDP       3     0  1     --       19     9      0.797 2021-04-03
#> 12 LAD       2     1  0.667 1.0      22    19      0.567 2021-04-03
#> 13 COL       1     2  0.333 2.0      19    22      0.433 2021-04-03
#> 14 SFG       1     2  0.333 2.0      13    15      0.435 2021-04-03
#> 15 ARI       0     3  0     3.0       9    19      0.203 2021-04-03

Created on 2022-01-02 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions