如何在R中读取具有日期名称的多个(循环).csv文件? [英] how to read multiple (loop) .csv files with a date name in R?

查看:559
本文介绍了如何在R中读取具有日期名称的多个(循环).csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个具有这些名称的.csv文件(从2016年1月1日到2016年12月31日)

I have several .csv files with these names (from 1 January 2016 to 31 december 2016)

    01012016.csv
    02012016.csv
    ...
    31122016.csv

我想通过循环使用read.csv(,但仍在考虑日期模式.

I want to use read.csv( by using a loop but still considering date patterns.

start<-as.Date("01-01-16")
end<-as.Date("31-12-16")
theDate<-start
­{read.csv(theDate,".csv")}

推荐答案

您可以使用list.files获取所有文件的名称,并为其提供所有文件所在的所有文件夹的路径:

You can get the names of all the files using list.files and giving it the path of all folder where all the files are located:

filenames = list.files('/path/to/datefiles/', pattern = "*.csv")

然后,您可以根据需要使用lapply遍历向量'filenames containing the names of files and apply read.csv to each of them and set additional parameters like header and stringsAsFactors`作为向量:

Then you can use lapply to iterate over the vector 'filenamescontaining the names of files and applyread.csvto each of them and set additional parameters likeheaderandstringsAsFactors` as TRUE or FALSE as required:

data = lapply(filenames,read.csv,header = TRUE,stringsAsFactors=FALSE)

这篇关于如何在R中读取具有日期名称的多个(循环).csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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