使用 R 将多个文件附加到数据框中 [英] Appending multiple files into a data frame using R

查看:29
本文介绍了使用 R 将多个文件附加到数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 10 000 多个文件附加到 R 中的数据框中.这项工作的第一步是从中抓取文件

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) {赋值(x = paste("data", i, sep = "_"), value =readHTMLTable((paste("webaddress_page=", i, sep = '')),which=1))}

这很好用,我有 10 000 个文件,data_1-data_10000.但是,我想将这些文件附加到 data.frame 中,但不确定如何进行?我要添加另一个数据步骤",还是可以在现有代码中添加?

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?

谢谢.

推荐答案

require(plyr)

files <- data_1-data_10000

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

确保阅读read.table 中的选项并适合您的数据.

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

编辑

让我们试试这个:

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 将多个文件附加到数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆