I have many arrays to fill in R, each of which has a different name in a specific pattern. The problem I'm having is that I can't figure out how to refer to the array names as variables, so I can refer to them in a systematic way inside a loop. They are created using this loop, for example.
for (i in c(40,45,50,55,57,60,65)){
assign(paste0('hdd_',as.character(i),'_365_z'), rep(NA,365))}
I want to fill each of these arrays, which are now named like this: hdd_40_365_z, hdd_45_365_z, etc.
Let's say I want to fill each of these arrays using a simple function like this:
my_func <- function(value,doy){
return(value * doy/2)} # doy = doy or 1:365
I can write a loop to do this:
for (j in c(40,45,50,55,57,60,65)){
for (k in 1:365){
# try to fill arrays one day at a time
paste0('hdd_',as.character(j),'_365_z')[k] <- my_func(j,k)}}
Obviously, this doesn't work. I've tried using the following to do this, but it's not working. There has to be a way to be able to do something like this, I would think, but I can't figure out the combination of eval, parse, as.name, or something else to do it. Any help would be great. Thank you.
eval(parse(text = paste0('hdd_',as.character(j),'_365_z')[k] <- my_func(j,k)))