I have scraped multiple tables from a basketball site using a for loop.
years <- c(2016:2021)
final_table <- {}
for(i in 1:length(years)){
url <- paste0("https://www.basketball-reference.com/friv/free_agents.cgi?year=",years[i])
past_free_agency_page <- read_html(url)
past_free_agency_webtable<- html_nodes(past_free_agency_page, "table")
past_free_agency_table <- html_table(past_free_agency_webtable, header = T)[[1]]
final_table <- rbind(final_table, past_free_agency_table)
}
This retrieves everything correctly, but I am trying to combine all of these tables as they are created. If you notice it is 5 total tables (Year 2016 - 2021).
There is one error that I am getting: I try to combine the table with rbind() at the end of the loop. It does not work. It says "the names do not match". I do not know of a clever way to fix this issue because I am new to working with loops, and I have tried turning the scraped table into a df with no success.
My next issue has to do with how the tables are combined. In the website links, one can see that the table has headers within it, that repeat the Master header exactly. The code treats it as another row, so it appears as an instance within each of the tables. I want these to be ignored.
The last issue has to do with making each of these rows unique, I want the respective year of each table to be a column in its own. For example, for the year 2016, I want the table to have a column that says 2016. I have tried something inside the loop, such as past_free_agency_table[,1] <- c(years[i]), I want to do this because some of these tables have the same players, and I want to be able to uniquely identify, which table is which.