0

I assigned a matrix to a name which varies with j:

j <- 2L
assign(paste0("pca", j,".FAVAR_fcst", sep=""), matrix(ncol=24, nrow=12)) 

This works very neat. Then I try to access a column of that matrix

paste0("pca", j,".FAVAR_fcst", sep="")[,2]

and get the following error:

Error in paste0("pca", j, ".FAVAR_fcst", sep = "")[, 2] : incorrect number of dimensions

I've tried several variations and combinations with cat(), print() and capture.output(), but nothing seems to work. I'm not sure what I have to search exactly for and couldn't find a solution. Can you help me?

nelakell
  • 189
  • 2
  • 13

1 Answers1

1

You can use get :

get(paste0("pca", j,".FAVAR_fcst", sep="")) # for the matrix

get(paste0("pca", j,".FAVAR_fcst", sep=""))[,2] # for the column
# [1] NA NA NA NA NA NA NA NA NA NA NA NA

An other solution would be to combine eval and as.symbol :

eval(as.symbol(paste0("pca", j,".FAVAR_fcst", sep="")))[,2]
# [1] NA NA NA NA NA NA NA NA NA NA NA NA
etienne
  • 3,648
  • 4
  • 23
  • 37