Read multiples files in R allocated in different directories but with the same name -
i trying solve little problem in r reading multiples files same name allocated in different directories.
i have 100 files named r04
, extension .xlsx
, allocated in 100 different directories, this:
file 1: c:\general data\month1\r04.xlsx
file 2: c:\general data\month2\r04.xlsx
.
.
.
file 100: c:\general data\month2\r04.xlsx
my problem can't read these files. maybe possible read for
, , due same name in 100 files don't know if possible name each 1 number related month, example in case of first file name should 01
due month 1, etc.
i use list.files
list files pattern. regular expression name files.
for example:
library(xlconnect) files.path <- list.files(pattern=".*r04.xlsx",full.names=true) setnames(lapply(files.path, function(x) read.xlsx(x,1)), gsub('.*/(.*)/r04.*','\\1_r04',files.path))
using data show how using gsub
here:
ll <- c("c:/general data/month1/r04.xlsx", "c:/general data/month2/r04.xlsx", "c:/general data/month3/r04.xlsx") gsub('.*/(.*)/r04.*','\\1_r04',ll) [1] "month1_r04" "month2_r04" "month3_r04"