Suppose I have two dependent categorical attributes A, B. I have a dataframe X that lists probabilities (or expected counts) for all combinations of all categories of A and B. Let's assume two categories in each attribute, the dataframe could then look like this:
X <- data.frame(A = c(1,1,2,2), B = 1:2, PROB = c(0.1,0.3,0.2,0.4)) X
A B PROB 1 1 1 .1 2 1 2 .3 3 2 1 .2 4 2 2 .4
Now I would like to add a B column to another dataframe Y that has only an A column. Y exists and has many more rows than X, and also additional columns. A synthetic Y could look like this:
Y <- data.frame(A=sample(2, size=10000, replace=TRUE), B=NA, C=sample(5, size=10000, replace=TRUE), D=sample(3, size=10000, replace=TRUE))
The contents of the new B column should be sampled at random using the conditional probabilities for B given the value of the A column.
I am new to R, and I was wondering whether this operation can be handled more elegantly than writing a loop that iterates over all A categories. Perhaps a dedicated model class for which
predict could be applied? Also, I lack the knowledge of statistical terminology -- I would appreciate any hint on how this kind of imputation (?) is referred to in the literature.