I want to create variable names on the fly inside a list and assign them values in R, but I am unable to get the desired result. Here is the logic of my code:
Upon the function call: dat_in <- readf(1,2)
, an input file is read based on a product and site. After reading, a particular column (13th, here) is assigned to a variable aot500
. I want to have this variable return from the function for each combination of product and site. For example, I need variables name in the list statement as aot500.AF
, aot500.CM
, aot500.RB
to be returned from this function. I am having trouble in the return
statement. There is no error but there is nothing in dat_in
. I expect it to have dat_in$aot500.AF
etc. Please inform what is wrong in the return
statement. Furthermore, I want to read files for all combinations in a single call to the function, say using a for loop and I wonder how would the return statement handle list of more variables.
prod <- c('inv','tot')
site <- c('AF','CM','RB')
readf <- function(pp, kk) {
fname.dsa <- paste("../data/site_data_",prod[pp],"/daily_",site[kk],".dat",sep="")
inp.aod <- read.csv(fname.dsa,skip=4,sep=",",stringsAsFactors=F,na.strings="N/A")
aot500 <- inp.aod[,13]
return(list(assign(paste("aot500",siteabbr[kk],sep="."),aot500)))
}