I have a loop that runs through a list of teams 1 by 1 and writes the output of the lineup to a .csv. So I have 31 individual .csv's named by the teamRename
that has 1 column and thats the player_data
. I would like to join all into one DF and export 1 DF to .csv. So 31 columns. Also the rows might not be the same amount in each team column/dataframe.
Here is the script so far:
library(rvest)
library(dplyr)
# teams
c("Anaheim-Ducks","Arizona-Coyotes","Boston-Bruins","Buffalo-Sabres",
"Calgary-Flames","Carolina-Hurricanes","Chicago-Blackhawks","Colorado-Avalanche",
"Columbus-Blue-Jackets","Dallas-Stars","Detroit-Red-Wings","Edmonton-Oilers",
"Florida-Panthers","Los-Angeles-Kings","Minnesota-Wild","Montreal-Canadiens",
"Nashville-Predators","New-Jersey-Devils","New-York-Islanders","New-York-Rangers",
"Ottawa-Senators","Philadelphia-Flyers","Pittsburgh-Penguins","San-Jose-Sharks",
"St-Louis-Blues","Tampa-Bay-Lightning","Toronto-Maple-Leafs","Vancouver-Canucks",
"Vegas-Golden-Knights","Washington-Capitals","Winnipeg-Jets"
) ->
teams
for (team in teams){
tryCatch({
#null df
roster <- NULL
# Get Website
url <- read_html(paste0('https://www.dailyfaceoff.com/teams/', team,'/line-combinations/stats'))
# Players in lineup
player_data <- url %>%
html_nodes('.player-name') %>%
html_text()
# remove numbers and # from name
player_data <- sub("\\s+#.*", "", player_data)
# Rename Team
team_rename <- gsub("-", "_", team, fixed=TRUE)
teamRename <- gsub("-", " ", team, fixed=TRUE)
# Print Team
print(team_rename)
# create roster dataframe
roster <- data.frame(Player = player_data)
# Bind rosters to roster_df
setwd('//LVS_DB/Users/Mike/Desktop/DB_LVS_SHARE/dailyfaceoffroster')
write_delim(roster, paste0(teamRename,'.csv'), delim = ',')
}, error = function(e) {message(paste0(e, "\n"))})
}