I'm working with some yearly and quarterly data, and I want to write a loop that cycles through and reads four quarters of data from four different CSVs each year, then run a script on them.
#prep strings
beg <- "fmli"
end <- ".csv"
fileList <- c()
for(i in 1996:2012){
yearString <- toString(i)
year <- substr(toString(i), 3,4)
for(i in 1:4){
quarter <- toString(i)
fileToRead <- paste(sep="", beg, year, quarter, end)
#add to list
#fileList <- append(fileToRead, i)
}
a1 <- read.csv(fileList[1])
a2 <- read.csv(fileList[2])
a3 <- read.csv(fileList[3])
a4 <- read.csv(fileList[4])
}
Essentially, I need this loop to end by assigning that year's 4 quarters to a1-a4. So that I can accomplish this:
#I want it to do this, but automated
#1996 quarterly files
a1 <- read.csv("fmli961.csv")
a2 <- read.csv("fmli962.csv")
a3 <- read.csv("fmli963.csv")
a4 <- read.csv("fmli964.csv")
#run the script
#1997 quarterly files
a1 <- read.csv("fmli961.csv")
a2 <- read.csv("fmli962.csv")
a3 <- read.csv("fmli963.csv")
a4 <- read.csv("fmli964.csv")
#run the script again
#etc, etc
I'm at a loss as to how to append to the list within the for loop -- I understand that this is probably not the most efficient way to go about writing this in R, but I wrote the script by assigning the quarters to DFs so I'm kinda married to this approach...