Hi I'm writing a function to read a file and returning a time series. I then need to assign this time series to a variable. I'm trying to do this in a terse way and utilise any functional programming features of R.
# read a file and returns the
readFile <- function( fileName , filePath){
fullPath <- paste(filePath, filename, sep='');
f <- as.xts(read.zoo(fullPath, format='%d/%m/%Y',
FUN=as.Date, header=TRUE, sep='\t'));
return(na.locf(f));
}
filePath <- 'C://data/'
# real list of files is a lot longer
fnames <- c('d1.csv', 'd2.csv','d3.csv');
varnames <- c('data1', 'data2', 'data3');
In the above piece of code I would like to initialise variables by the name of data1, data2, data2 by applying the readfile function to fnames and filepath (which is always constant).
Something like :
lapply( fnames, readFile, filePath);
The above doesnt work, of course and neither does it do this dynamic variable assignment that I'm trying to achieve. Any R functional programming gurus out there that could guide me ?
The working version of this would look something like :
data1 <- readFile( 'd1.csv', filepath);
data2 <- readFile( 'd2.csv', filepath);
YIKES