I have a probability tables stored as arrays (outputted by bnlearn). The number of dimensions is always equal to the number of parents + 1. I always want to fetch the first the dimension and the dimnames of the remaining dimensions is stored in vals below.
Right now, I'm doing this:
vals = avatar[cpt$parent] # c('a','b')
eval_str = paste( "cpt$prob[,\'"
, paste(vals, collapse="\',\'")
, "\']", sep=""
)
row = eval(parse(text=eval_str))
In the three dimensional case the code evals to this: row=cpt$prob[,'a','b']
Any idea on how to do this without using eval?
Further Explanation
I would like to subset an array by a variable with the index to subset by. For example:
a1 <- array(1:81, c(3,3,3,3))
I can subset with a1[,2,2,3] directly to get 67 68 69. But if I had a variable vals <- c(2,2,3) and try a1[,vals] I get an error. Or if I try a1[vals] it will evaluate to the equivalent of a1[c(2,2,3)].
I have also tried do.call since it allows for do.call('[', ...). Or with mapply(function(...) a1[...], ..) to no avail.
I would like to have a variable of indices and subset based on those. But NOT the positions as one would normally do.
The issue appears to be in the argument structure of '[' which is x[i, j, ...]. I'm looking for j and ... to be supplied by an outside variable.
For now, I am using the eval(parse(.. method to specify the spacing and comma placement. I can then turn a1[,vals] into a1[,2,2,3] but I would like to avoid that method.