R Apply function to list and create new dataframe

1k Views Asked by At

I am wanting to retrieve data from several webpages that is in the same place on all the pages and put it all in one data frame.

I have the following code attempt:

library(XML)
library(plyr)

**##the urls**
raceyears<-list(url2013,url2012,url2011)

**##function that is not producing what I want**
raceyearfunction<-function(x){
page<-readLines(x)
stats<-page[10:19]
y<-read.table(textConnection(stats))
run<-data.frame(y$V1,y$V2)
colnames(run)<-c("Country","Participants")
rbind.fill(run)
}

data<-llply(raceyears,raceyearfunction)

This places all the data in multiple columns (two columns for each webpage) but I am wanting all the data in two columns (Participants, Country) one data frame not many columns in one data frame.

I haven't found a question quite like this already on the site but am open to follow a link. Thank you in advance.

1

There are 1 best solutions below

1
Cron Merdek On BEST ANSWER

You need to use rbindlist outside of raceyearfunction. Let it return(run) without rbind.fill(run).

You can use ldply instead, then it will return binded data.frame already.

library(XML)
library(plyr)

raceyears <- list(url2013,url2012,url2011)

raceyearfunction<-function(x)
{
    page <- readLines(x)
    stats <- page[10:19]
    y <- read.table(textConnection(stats))
    run <- data.frame(y$V1,y$V2)
    colnames(run) <- c("Country","Participants")
    return(run)
}
data<-ldply(raceyears, raceyearfunction)