[R] How to make combination data

Liaw, Andy andy_liaw at merck.com
Tue Apr 19 15:48:54 CEST 2005


Is something like this what you're looking for?

fixSome <- function(..., fixed) {
    fList <- list(...)
    for (i in fixed) fList[[i]] <- fList[[i]][1]
    do.call("expand.grid", fList)
}

> age       <- c("young","mid","old")
> married   <- c("no","yes")
> income    <- c("low","high","medium")
> gender    <- c("female","male")
> 
> age.income.dat <- fixSome(age, married, income, gender, fixed=c(2, 4))
> age.income.dat
   Var1 Var2   Var3   Var4
1 young   no    low female
2   mid   no    low female
3   old   no    low female
4 young   no   high female
5   mid   no   high female
6   old   no   high female
7 young   no medium female
8   mid   no medium female
9   old   no medium female

Andy

> From: Jan Sabee
> 
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 
>




More information about the R-help mailing list