'Computing conditional probabilities from three dimensional frequency table in R

I have a three dimensional frequency table. Let's say the three dimensions are var1, var2 and var3. I would like to compute a new three dimensional table where each entry is the probability of var1 = x knowing that var2 = y and var3 = z. I could of course iterate through the table and compute the values, but I don't think that would be the "native" way to go in R.

r


Solution 1:[1]

I believe you are looking for vectorization in data frames. It would help commentators if you provided example data and your function. Here's a toy example:

df <- bind_cols("var2" = 1:10, "var3" = 11:20) 

I assume you are using some sort of probability function that depends upon "var2" and "var3" to calculate "var1".

df <- df %>% mutate(var1 = probability.fuction(var2, var3))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SeaJane