I've build a for-loop that should store the first and surname of a soccer player (scraped from a website) in separate columns, but the for-loop keeps overwriting all stored results with the most recent output.
noplayers <- 3 # the amount of players I want to run the loop for while testing my code
playeridtest <- playerid[1:noplayers] # assign the three IDs to a vector
playernames <- rep(NA, noplayers)
playernames <- as.data.frame(playernames) # Create an empty data frame to store results in
playernames$id <- playeridtest # Add the three player IDs to the ID column
for(i in playeridtest){
scoresway <- paste("http://www.scoresway.com?sport=soccer&page=person&id=",i, sep="")
scoresway <- read_html(scoresway)
urlnodescorefirst <- html_node(scoresway, "dd:nth-child(2)")
urltextscorefirst <- html_text(urlnodescorefirst)
playernames$first <- urltextscorefirst
urlnodescoresur <- html_node(scoresway, "dd:nth-child(4)")
urltextscoresur <- html_text(urlnodescoresur)
playernames$sur <- urltextscoresur
}
Using the test vector of 3 player IDs to find the first and surnames, it keeps storing the name of the third player three times. (Lahm's ID = 14)
id first sur
4 Philipp Lahm
11 Philipp Lahm
14 Philipp Lahm
for(i in seq_along(playeridtest)) { # Note change here
scoresway <- paste("http://www.scoresway.com?sport=soccer&page=person&id=",playeridtest[i], sep="")
scoresway <- read_html(scoresway)
urlnodescorefirst <- html_node(scoresway, "dd:nth-child(2)")
urltextscorefirst <- html_text(urlnodescorefirst)
playernames$first[i] <- urltextscorefirst
urlnodescoresur <- html_node(scoresway, "dd:nth-child(4)")
urltextscoresur <- html_text(urlnodescoresur)
playernames$sur[i] <- urltextscoresur
}
Result:
playernames
playernames id first sur
1 NA 4 Maarten Stekelenburg
2 NA 11 Robert Huth
3 NA 14 Philipp Lahm
The column of playernames$playernames is a result of the code you included at the top. Just skip these two lines, and replace with the third:
# playernames <- rep(NA, noplayers)
# playernames <- as.data.frame(playernames)
playernames<-NULL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.