I've been having some trouble adding in titles to created data frames with for loops. This is the basic structure of the data:
> head(year2019)
date day_of_week away_team away_team_game_number home_team home_team_game_number away_score home_score day_night park game_length away_AB away_H away_2B
1 2019-03-20 Wed SEA 1 OAK 1 9 7 N OAK01 204 31 7 1
2 2019-03-21 Thu SEA 2 OAK 2 5 4 N OAK01 267 43 9 4
3 2019-03-28 Thu PIT 1 CIN 1 3 5 D CIN09 174 31 5 0
4 2019-03-28 Thu ARI 1 LAN 1 5 12 D LOS03 169 33 9 4
5 2019-03-28 Thu COL 1 MIA 1 6 3 D MIA02 175 36 9 5
6 2019-03-28 Thu SLN 1 MIL 1 4 5 D MIL06 156 32 5 0
With this code I've been able to create dataframes for each team:
for (i in teams) {
assign(i, year2019 %>% filter(away_team == i | home_team == i))
}
With teams <- c("ANA", "ARI", "ATL", ...)
However I want to run this with creating both home and away teams. I've tried some of the following but nothing has worked so far:
for(i in teams) {
i_home <- i %>% filter(home_team == i)
i_away <- i %>% filter(away_team == i)
}
Or
for (i in teams) {
i1 <- filter(year2019, home_team == i)
i2 <- filter(year2019, away_team == i)
}
Any advice on how to properly introduce added names for i in this?