I have been wondering if anybody knows a way to create a loop that loads files/databases in R. Say i have some files like that: data1.csv, data2.csv,..., data100.csv.
In some programming languages you one can do something like this data +{ x }+ .csv the system recognizes it like datax.csv, and then you can apply the loop.
Any ideas?
Sys.glob()
is another possibility - it's sole purpose is globbing or wildcard expansion.
dataFiles <- lapply(Sys.glob("data*.csv"), read.csv)
That will read all the files of the form data[x].csv
into list dataFiles
, where [x]
is nothing or anything.
[Note this is a different pattern to that in @Joshua's Answer. There, list.files()
takes a regular expression, whereas Sys.glob()
just uses standard wildcards; which wildcards can be used is system dependent, details can be used can be found on the help page ?Sys.glob
.]