I have two 2×2 data frames. Each column in each data frame is a factor.
I want to create a 2×8 data frame that contains each factor and the interactions between factors.
Here is an example:
df1 <- data.frame(V1 = factor(c('a', 'b')), V2 = factor(c('c', 'd')))
df2 <- data.frame(V3 = factor(c('e', 'f')), V4 = factor(c('g', 'h')))
df.combined <- combine(df1, df2)
Where df.combined would be
V1 V2 V3 V4 V1:V3 V1:V4 V2:V3 V2:V4
a c e g a:e a:g c:e c:g
b c f h b:f b:h d:f d:h
(I don’t want the V1:V2 or V3:V4 interactions. Not needing those interactions is just in the nature of the problem that I face.)
Is there a succinct way to get df.combined in R?
If the colons in the name are not required, it is just one line of code that takes care of both column binding the two data frames and creating the interactions. Using your two data frames:
which gives
If you need the colons in the names, another oneliner will change periods to colons:
leaving the final results