Look at a string in an arbitrary sized array

I have probability tables stored as arrays (deduced by bnlearn). The number of measurements is always equal to the number of parents + 1. I always want to get the first measurement, and the dimnamesremaining measurements will be stored in valsbelow.

Now I am 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 proceeds to the following: row=cpt$prob[,'a','b']

Any idea on how to do this without using eval?

Further explanation

I would like a subset of an array of variable with an index for the subset. For example:

a1 <- array(1:81, c(3,3,3,3))

I can multiply 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 the equivalent a1[c(2,2,3)].

do.call, do.call('[', ...). mapply(function(...) a1[...], ..) .

. , .

'[', x[i, j, ...]. j ... .

eval(parse(.. . a1[,vals] a1[,2,2,3], .

+4
1

, :

vals <- c(2, 2, 3)

, ( eval):

a1[,2,2,3]
# [1] 67 68 69

, , :

"["(a1, , 2, 2, 3)
# [1] 67 68 69

do.call("[", c(list(a1, substitute()), as.list(vals)))
# [1] 67 68 69
+4

Source: https://habr.com/ru/post/1610628/


All Articles