0

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"))})
}
Michael T Johnson
  • 575
  • 2
  • 9
  • 22
  • 4
    This is a bad procedure, don't do it. [Make a list of data frames instead, and combine them all at the end](https://stackoverflow.com/a/24376207/903061). Once you have your list of data frames, you can combine them all to one with `Reduce(cbind, list_of_data)`. – Gregor Thomas Oct 01 '18 at 20:34
  • Though, if *"Also the rows might not be the same amount in each team column/dataframe"*, then you'll need to decide what to do with the extra/missing rows. I'd suggest a long format instead of a wide format, like `dplyr::bind_rows(list_of_data)` or `data.table::rbindlist(list_of_data)`. – Gregor Thomas Oct 01 '18 at 20:37

0 Answers0