Appending multiple files into a data frame using R

不羁的心 提交于 2019-12-11 13:04:39

问题


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) 
}    


来源:https://stackoverflow.com/questions/22053090/appending-multiple-files-into-a-data-frame-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!