> with(df, interaction(A, B)) [1] a.b a.x x.x x.x Levels: x.x a.x x.b a.b > A.B <- factor(with(df, interaction(A, B))) > A.B [1] a.b a.x x.x x.x Levels: x.x a.x a.b > model.matrix(~ A.B, df) (Intercept) A.B[T.a.x] A.B[T.a.b] 1 1 0 1 2 1 1 0 3 1 0 0 4 1 0 0 attr(,"assign") [1] 0 1 1 attr(,"contrasts") attr(,"contrasts")$A.B [1] "contr.Treatment" >