递归读取并打印csv文件的状态

时间:2016-09-28 16:46:45

标签: r csv

我是R的初学者,最近从STATA过渡到R.所以,这是一场艰苦的战斗。我能够编写一个矢量化命令来递归地读取csv文件,如Sapply vs. Lapply while reading files with factors所述。这是我的代码:

filenames<-list.files(path="~/Documents/R Programming/Data/",pattern=".csv")
appended_filename<-sapply(filenames, function(x) paste("~/Documents/R Programming/Data/",x,sep = ""))

Merged_file<-do.call(rbind,lapply(appended_filename,read.csv))

但是,我有大约50多个文件。挑战在于我无法知道读取任何文件是否存在问题。有没有办法打印状态,如"1 2 ..."(我不是在寻找任何漂亮的东西......只是对正在发生的事情的更新)只知道已经读取了多少文件?

我是初学者,所以我不知道如何添加一个可以让我看到一些可见性的函数。作为后备选项,我在运行上述命令之前手动编写了read.csv()函数来测试和检查每个文件,最后是rbind()函数。这非常痛苦。

2 个答案:

答案 0 :(得分:2)

您可以在lapply中使用匿名函数,就像上面的sapply一样。然后在函数中,您可以打印出文件名,读入它,做任何你想做的事情。因此,不是将read.csv应用于每个attachment_filename,而是可以执行以下操作:

do.call(rbind, lapply(appended_filename, function(x) {print(x); read.csv(x)}))

您还可以使用方法rbind.fill(在plyr库中)来组合数据帧列表。这比do.call更清洁。

rbind.fill(lapply(appended_filename, function(x) {print(x); read.csv(x)}))

答案 1 :(得分:1)

进度条可能是更好的方法:

library(purrr)
library(dplyr)

td <- tempdir()

# Make 100 copies of mtcars in a temporary directory
walk(1:100, ~write.csv(mtcars, file.path(td, sprintf("mtcars%02d.csv", .)), row.names=FALSE))

# Get a list of the files. dir() == list.files(), just shorter
fils <- dir(td, pattern=".csv", full.names=TRUE)

# Inspect the list
head(fils)
## [1] "/var/folders/3r/zg9pcxys4dqg4j7_bqbn3c0h0000gn/T//RtmpW0AVZ2/mtcars01.csv"
## [2] "/var/folders/3r/zg9pcxys4dqg4j7_bqbn3c0h0000gn/T//RtmpW0AVZ2/mtcars02.csv"
## [3] "/var/folders/3r/zg9pcxys4dqg4j7_bqbn3c0h0000gn/T//RtmpW0AVZ2/mtcars03.csv"
## [4] "/var/folders/3r/zg9pcxys4dqg4j7_bqbn3c0h0000gn/T//RtmpW0AVZ2/mtcars04.csv"
## [5] "/var/folders/3r/zg9pcxys4dqg4j7_bqbn3c0h0000gn/T//RtmpW0AVZ2/mtcars05.csv"
## [6] "/var/folders/3r/zg9pcxys4dqg4j7_bqbn3c0h0000gn/T//RtmpW0AVZ2/mtcars06.csv"

# Use a progress bar based on total # of files to read
pb <- progress_estimated(length(fils))

map_df(fils, function(x) {  # map_df will automagically append all the data frames together
  pb$tick()$print()         # increment the progress bar
  read.csv(x)
}) -> df

# see what we've got
glimpse(df)
## Observations: 3,200
## Variables: 11
## $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19....
## $ cyl  <int> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 1...
## $ hp   <int> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, ...
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
## $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
## $ vs   <int> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
## $ am   <int> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
## $ gear <int> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
## $ carb <int> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...

# cleanup those files
walk(fils, unlink)