Reputation: 462
I have a subdirectory of my project folder containing 50 .csv files. I know how to load them using a for loop and an absolute file path.
However, I would like to load them using a relative file path, so its easier to hand the project to others. Below you see the code, in which I already wrote the relative path, which is working if an absolute path is used.
list.filenames.field <- list.files(path = "./data_field/", pattern = ".csv$")
list.data.field <- list()
for (i in 1:length(list.filenames.field))
{
list.data.field[[i]] <- read.csv("./data_field/list.filenames.field[[i]]", header = T, stringsAsFactors = FALSE)
}
How can a relative file path can be used when reading files using a for loop?
Upvotes: 0
Views: 539
Reputation: 462
I found a solution that works for me, not sure if it is the most efficient though.
listFilenames <- list.files(path = "./data_field/", pattern = ".csv$")
listData<- list()
path <- list()
for (i in 1:length(listFilenames ))
{
path[[i]] <- paste0("./data/",listFilesnames[[i]])
listData[[i]] <- read.csv("./data/listFilenames [[i]]", header = T, stringsAsFactors = FALSE)
}
Upvotes: 1