0

我正在尝试从 .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 循环,我就找不到解决方法...

任何建议表示赞赏

4

1 回答 1

0

谢谢!我试过 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"))
    )))
于 2016-10-20T20:18:29.503 回答