0

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

user1480926
  • 634
  • 5
  • 12
  • 1
    possible duplicate of [How to name variables on the fly in R?](http://stackoverflow.com/questions/2679193/how-to-name-variables-on-the-fly-in-r) – shadow Sep 29 '14 at 14:09
  • Thanks shadow - yes its partially a duplicate, but I'm also trying to understand how the lapply can be used to achieve this for many variables since I'm new to FP. – user1480926 Sep 29 '14 at 14:11
  • 1
    If you really want to do that (read the comments in the linked question: DON'T DO THAT), you could use `mapply(FUN=assign, x=varnames, value=data, MoreArgs=list(envir=.GlobalEnv))` – shadow Sep 29 '14 at 14:19

1 Answers1

3

Constructing many variables with specified names is a somewhat common request on SO and can certainly be managed with the assign function, but you'll probably find it easier to handle your data if you build a list instead of multiple variables. For instance, you could read in all the results and obtain a named list with:

lst <- setNames(lapply(fnames, readFile, filePath), varnames)

Then you could access your results from each csv file with lst[["data1"]], lst[["data2"]], and lst[["data3"]].

The benefit of this approach is that you can now perform an operation over all your time series variables using lapply(lst, ...) instead of looping through all your variables or looping through variable names and using the get function.

josliber
  • 43,891
  • 12
  • 98
  • 133