问题描述
我有几个具有这些名称的.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 apply
read.csvto each of them and set additional parameters like
headerand
stringsAsFactors` as TRUE or FALSE as required:
data = lapply(filenames,read.csv,header = TRUE,stringsAsFactors=FALSE)
这篇关于如何在R中读取具有日期名称的多个(循环).csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!