This is the sort of thing I was thinking:
dat <- read.table(text = "deaths sex race smokes pyears
10 Female White 0 1410
14 Male White 1 1974
14 Female Black 0 1974
16 Male Black 1 2256
17 Male Black 0 2397
18 Female NA 1 2538
19 NA Black 0 2679
20 Female White 1 2820
20 Female Black 0 2820
21 Male Black 1 2961",header = TRUE,sep = "")
dat$sex <- factor(dat$sex,exclude = NULL)
dat$race <- factor(dat$race,exclude = NULL)
with(dat,interaction(sex,race))
[1] Female.White Male.White Female.Black Male.Black Male.Black Female.NA NA.Black Female.White Female.Black
[10] Male.Black
Levels: Female.Black Male.Black NA.Black Female.White Male.White NA.White Female.NA Male.NA NA.NA
It looks like you wanted to include the NAs, rather than drop them, hence the explicit factor
calls. Obviously, the resulting factor can be converted to integers using as.integer
, although the actual numbers won't likely be in the order you specified, since R will order things alphabetically, rather than how they appear in your data frame.