追加多个文件合并,使用R数据帧 [英] Appending multiple files into a data frame using R

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

问题描述

我想超过10 000文件追加到河中的一个数据帧在此作业的第一步是从

刮文件

的(我在1:10000){
  分配(X =膏(数据,我09月=_),值=
           readHTMLTable((糊(webaddress_page =,我,九月='')),其= 1))
}

这工作得很好,我有10个文件,_1,data_10000。不过,我想这些文件追加到一个data.frame,但不知道如何着手?难道我再添数据步,或者它可以将现有的code之内呢?

感谢。


解决方案

 要求(plyr)文件<  -  _1,data_10000DAT<  -  ldply(文件,功能(FN)data.frame(函数read.table(FN,标题= FALSE)))

请务必阅读函数read.table 选项,适合您的数据。

修改

让我们试试这个:

  DAT<  -  data.frame()为(i的1:10000){
    的DAT pre&下; - readHTMLTable((糊(webaddress_page =,我,九月='')),其= 1)
    N'LT; - 最大(长(DAT),长度(DAT pre))。
    长度(DAT)< - N
    (DAT pre)长度LT; - N
    DAT< - cbind(DAT,DAT pre)。
}

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.

解决方案

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

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