I have a multidimensional contingency table of 4 variables. And I need to get the values out of that table, using a length-4 vector. The whole reason for doing this is to be able to get a huge list of such vectors and find a corresponding output from the contingency table.
Is there a good way to convert c(1,2,3,4)
into my.table[1,2,3,4]
?
Here's an example of what I'm currently doing with only one vector as an input:
v = c(1,2,3,4)
my.table = table(my.data.frame[,c("x","y","z","w")])
eval(str2lang(paste0("my.table[", paste0(v, collapse = ", "), "]")))
And this is what I'm doing with a big list of input vectors, which is in my case a data frame:
data.frame.of.inputs = data.frame(x = sample(1:4, 100, T), y = sample(1:4, 100, T),
z = sample(1:4, 100, T), w = sample(1:4, 100, T))
my.table = table(some.other.data.frame[,c("x","y","z","w")])
apply(data.frame.of.inputs,1,function(rw)
eval(str2lang(paste0("my.table[", paste0(rw, collapse = ", "), "]"))))
Note: to make this code run you can initialize my.data.frame
and some.other.data.frame
with the same statement:
data.frame(x = sample(1:4, 100, T), y = sample(1:4, 100, T),
z = sample(1:4, 100, T), w = sample(1:4, 100, T))
I don't like my current solution not just because it is ugly, but also because it seems to be a very long way of doing something simple.
Also, as a side-quest of mine: is there a way to subset a table via attributes(my.table)$variable.name?
From ?"["
:
When indexing arrays by ‘[’ a single argument ‘i’ can be a matrix with as many columns as there are dimensions of ‘x’; the result is then a vector with elements corresponding to the sets of indices in each row of ‘i’.
With one vector:
my.table[rbind(v)]
(i.e., convert the vector into a one-row matrix)
Multiple inputs:
my.table[as.matrix(data.frame.of.inputs)]