Appending multiple files into a data frame using R

2019-08-12 22:55发布

问题:

I am trying to append more than 10 000 files into a data frame in R. The first step in this job was to scrape files from

for(i in 1:10000) { assign(x = paste("data", i, sep = "_"), value = readHTMLTable((paste("webaddress_page=", i, sep = '')),which=1)) }

This works just fine, and I have 10 000 files, data_1-data_10000. However, I would like to append these files into a data.frame, but not sure how to proceed? Do I add another "data step", or maybe it is possible to do within the existing code?

Thanks.

回答1:

require(plyr)

files <- data_1-data_10000

dat <- ldply(files, function(fn) data.frame(read.table(fn, header = FALSE)))

Make sure to read the options in read.table and fit to your data.

EDIT

Let's try this:

dat <- data.frame()

for(i in 1:10000) { 
    dat.pre <- readHTMLTable((paste("webaddress_page=", i, sep = '')), which=1)
    n <- max(length(dat), length(dat.pre))
    length(dat) <- n
    length(dat.pre) <- n
    dat <- cbind(dat, dat.pre) 
}    


标签: r append rbind