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
}