lapply(c('1234' ,'1345','1456','1560'),function(x){
sources.files <- list.files(path=TF,
recursive=T,
pattern=paste('*09061*',x,'*.csv',sep='')
,full.names=T)
## You read all files with the id and bind them
dat <- do.call(rbind,lapply(sources.files,read.csv))
### write the file for the
write(dat,paste('agg',x,'.csv',sep='')
}
在R中,只需在
list.files
中设置recursive = T
。在实际上,在处理目录时指定递归是一种通用方法,它可以在操作系统(包括Linux和Windows)中使用
rm -rf
等命令进行命令行操作,并适用于R中的多个函数这篇文章有一个很好的例子:
How to use R to Iterate through Subfolders and bind CSV files of the same ID?
他们的示例(不同之处在于他们对目录/子目录搜索的结果所做的操作)是:
因此,对于您
pattern = '.dta'
,只需将基本目录设置在path
中。在考虑使用baser的list.files()作为递归参数指定在子目录中搜索。您还需要全名设置为返回文件引用的绝对路径。在
因此,将模式设置为查找
.dta
扩展(即Stata数据集),然后运行read-in和write函数:以及Python pandas中的对应项,它内置了read and write stata files方法:
^{pr2}$相关问题 更多 >
编程相关推荐