[R] How to make combination data

Jan Sabee jan.sabee at gmail.com
Tue Apr 19 14:58:47 CEST 2005


Dear R-user,
I have a data like this below,

  age       <- c("young","mid","old")
  married   <- c("no","yes")
  income    <- c("low","high","medium")
  gender    <- c("female","male")

I want to make some of combination data like these,

  age.income.dat <- expand.grid(age, married[-c(2)], income, gender[-c(2)])
  age.income.dat
  age.married.dat <- expand.grid(age, married, income[-c(2:3)], gender[-c(2)])
  age.married.dat
  married.gender.dat <- expand.grid(age[-c(2:3)], married,
income[-c(2:3)], gender)
  married.gender.dat
  married.income.gender.dat <- expand.grid(age[-c(2:3)], married,
income, gender)
  married.income.gender.dat
  age.income.gender.dat <- expand.grid(age, married[-c(2)], income, gender) 
  age.income.gender.dat

On these data the first cell in each variable are fixed (no change).
Because I only make some of variables what I need, How can I make
these with a simple functions.

Best reagrds,
Jan Sabee




More information about the R-help mailing list