I'm not sure whether by "dummy variable" you want 0/1 indicator variables (in which you would have 18 dummy variables) or whether you want a single factor with 18 levels. Sounds like the latter. (Actually, paste
would work as well as interaction
, although interaction
is a bit more self-describing.)
> ff <- expand.grid(agegroup=factor(c("<20","20-30",">30")),
disease.level=factor(0:2),performance=factor(c("<60",">=60")))
> combfac <- with(ff,interaction(agegroup,disease.level,performance))
> combfac
[1] <20.0.<60 20-30.0.<60 >30.0.<60 <20.1.<60 20-30.1.<60
[6] >30.1.<60 <20.2.<60 20-30.2.<60 >30.2.<60 <20.0.>=60
[11] 20-30.0.>=60 >30.0.>=60 <20.1.>=60 20-30.1.>=60 >30.1.>=60
[16] <20.2.>=60 20-30.2.>=60 >30.2.>=60
18 Levels: <20.0.<60 20-30.0.<60 >30.0.<60 <20.1.<60 20-30.1.<60 ... >30.2.>=60
If you want to use all the variables in the data frame to create the interaction you can use do.call(interaction,ff)
.
If you did want the dummy variables you would do model.matrix(~combfac-1)
to get them.