【问题标题】:R Importing several fwf into one dataframeR将几个fwf导入一个数据帧
【发布时间】:2016-10-20 20:02:14
【问题描述】:

我正在尝试从 .txt (fwf) 格式的 11 个文件创建 DF,并希望使用 apply。我检查并找到了一些关于 read.csv 的好建议,但不是 fwf(因为你必须指定宽度)

这是我的数据的样子:

AA F AD PR PA POSICION T  PESO (KG.)   UNIDADES    V.ESTADISTI  DF
10 E 01 01 AT 02101981 3 000000000053 000000000000 000000035541 01
10 E 01 01 AT 03027000 3 000000000000 000000000000 000000005940 01
10 E 01 01 AT 15091010 3 000000000012 000000000000 000000019500 01
10 E 01 01 AT 16010091 3 000000000154 000000000000 000000105195 01
10 E 01 01 AT 16024919 3 000000000015 000000000000 000000004724 01
10 E 01 01 AT 16051000 3 000000000043 000000000000 000000464400 01
10 E 01 01 AT 16059090 3 000000000006 000000000000 000000020234 01

并将其保存在一个目录中,这里是名称示例:

Files<-c("tr00an24.txt" "tr00an38.txt" "tr00an43.txt")

到目前为止,这适用于单个文件:

Trade00<-read.fwf(Files[1],
                widths = c(2, 2, 3, 3, 3, 9, 2, 13, 13, 13, 3),
                colClasses = c(rep("character", 7), rep("numeric", 3), 
                                 "character"),
                header = TRUE,
                col.names = c("AA", "F", "AD", "PR", "PA", "POSICION", "T",  
                            "PESO (KG.)",   "UNIDADES",    "V.ESTADISTI",  
                            "DF")
                )

但是,当我尝试时:

   Trade00<-lapply(Files_00[1:2], read.fwf,
                      widths = c(2, 2, 3, 3, 3, 9, 2, 13, 13, 13, 3),
                      colClasses = c(rep("character", 7), rep("numeric", 3), 
                                     "character"),
                      header = TRUE,
                      col.names = c("AA", "F", "AD", "PR", "PA", "POSICION", "T",  
                                    "PESO (KG.)",   "UNIDADES",    "V.ESTADISTI", "DF") 
) 

它返回一个 DF 列表而不是一个 DF。我知道我必须以某种方式告诉 R 将每个新的 DF 附加到现有的,但我无法找到如何。

当然是很容易做到的事情,但是如果不使用 for 循环,我无法找到解决方法...

任何建议表示赞赏

【问题讨论】:

  • do.call(rbind, listOfDataFrames) 会将所有数据帧附加在一起。
  • readr 有一些很好的实用程序来处理您可能会查看的固定宽度文件。上述方法的替代方案是dplyr::bind_rows

标签: r apply read.fwf


【解决方案1】:

谢谢!我试过 do.call 并且效果很好:

Trade00 <- do.call("rbind", lapply(Files_00[1:2], function(fn) 
    data.frame(Filename=fn, read.fwf(fn,
                                     widths = c(2, 2, 3, 3, 3, 9, 2, 13, 13, 13, 3),
                                     colClasses = c(rep("character", 7), rep("numeric", 3), 
                                                    "character"),
                                     header = TRUE,
                                     col.names = c("AA", "F", "AD", "PR", "PA", "POSICION", "T",  
                                                   "PESO (KG.)",   "UNIDADES",    "V.ESTADISTI",  
                                                   "DF"))
    )))

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2019-04-09
    相关资源
    最近更新 更多