I have read in data from an excel file, and made it into a vector. I then made it into a 3d array.
The vector that forms the array, and also inside the array now has in it characters, like this:
D <- c('g', 't', NA, 'd')
nPeriods = 3
column.names = c('aaa', 'bbb')
row.names = c('jjj', 'hhh')
threeD.names = c(1:nPeriods)
E = array(c(D), dim=c(2, 2, nPeriods),
dimnames = list(row.names, column.names, threeD.names))
However, now I want to assign 'g', 't', etc as variables with starting values, e.g.
g = 5
t = 2
d = 7
But the only way I know how to do this is by manually figuring out which element of the array it is and assigning it like that, e.g.
E[1,1,]=5
When it's a large matrix that's going to be difficult and annoying to find the corresponding position every time I refer to an element. I know that each element of the vector (that is not NA) is unique, so I wondered, is there a shortcut for referring to each element of the array? (Maybe something in the apply family? but there are so many of them).
I will also need to refer later to them to loop over nPeriods. Yesterday someone showed me I can do this with:
for (i in 2:nPeriods){
C[1,1,i]=C[1,1,i-1]*2
}
But in real life my matrix might be quite large so I'd rather just be able to refer with d, t, etc.