Trying to scrape individual game stats over a players career from basketball-reference.com (which is working), but I want to add the players name to the resulting df corresponding with the individual game results. For example, the first loop would just repeat "Kareem Abdul-Jabbar" 86 times for the 86 rows generate by the scrape. I'm trying to get the next loop to add to existing column named "Player_Name" using the cbind fill method, but cbind is instead creating a new column with each loop. Any advice on how to get the players name into a single column would be much appreciated.
library(rvest)
library(dplyr)
# Create df of players to be scraped
#########################################################################
players = data.frame(player_name = c(rep("Kareem Abdul-Jabbar",each=20),
rep("Karl Malone",each=19)),
player_id = c(rep("abdulka01",each=20),
rep("malonka01",each=19)),
initial = c(rep("a",each=20),
rep("m",each=19)),
year = c(seq(1970,1989,by=1),
seq(1986,2004,by=1)))
# Scrape data and stack in a df
#########################################################################
output <- data_frame()
for (i in 1:2){
url <- paste0("https://www.basketball-reference.com/players/",
players[i,3],"/",players[i,2],"/gamelog/",players[i,4])
webpage <- read_html(url)
temp <- webpage %>%
html_nodes("#pgl_basic") %>%
html_table()
player_name=players[i,1]
output <- cbind(bind_rows(output, temp),player_name)
}