I have a dataset with the variables sex
and navigation
. The sex
variable has male
and female
as values. The "navigation" variable has menu
and tags
as values.
I want to create a new variable with the values male_menu
, male_tags
, female_menu
& female_tags
as those are the possible combination of the two existing variables.
How can I create such a new variable in R
and include it in the original dataset?
I understood what you wanted somewhat differently than @zach. Here I use the interaction
function to create a new factor with the four levels you specified. Here using some dummy data
set.seed(42)
sex <- sample(c("Male","Female"), 20, replace = TRUE)
navigation <- sample(c("menu","tags"), 20, replace = TRUE)
interaction(sex, navigation)
the last line gives
> interaction(sex, navigation)
[1] Female.tags Female.menu Male.tags Female.tags Female.menu Female.tags
[7] Female.menu Male.tags Female.menu Female.tags Male.tags Female.tags
[13] Female.menu Male.tags Male.menu Female.tags Female.menu Male.menu
[19] Male.tags Female.tags
Levels: Female.menu Male.menu Female.tags Male.tags
Is that what you wanted?