Search code examples
rrvestxml2

How to add a new row to a HTML table


I'm scraping data for historical Dog race results for a Uni project. How can i add a Column to the table with the elements from a different table? Specifically the table before it consisting of columns 3 and 4 so add the that information to create a 13th and 14th columns on the table below and populate ever row with that information. [Row from different table[1]

Here is the code i Have.

library(xml2) 
library(rvest) 
web<- read_html('https://www.thegreyhoundrecorder.com.au/results/bendigo/61001')
tables<- html_nodes(web, 'table') 
tab1<- html_table(tables, fill = TRUE)

Solution

  • We can remove first the list elements which have 1 row and 2 columns.

    We do this using Filter.

    remain_tab <- Filter(function(x) !(nrow(x) == 1 & ncol(x) == 2), tab1)
    #Probably it is simpler to just remove 1st 12 elements if you know 
    #they are the problem and don't want to include in the final dataset.
    #remain_tab <- tab1[-c(1:12)]
    

    and then using Map we change alternate data.

    remain_tab[c(FALSE, TRUE)] <- Map(function(x, y) 
                           {y[paste0('X', c(13, 14))] <- x[c(3, 4)];y}, 
                           remain_tab[c(TRUE, FALSE)], remain_tab[c(FALSE, TRUE)])