Greetings useRs, What is the easiest way to create a design matrix of several factor variables? Function gendata in Design seems to do that for a fitted model, but how to do that only on several factor vectors?? The result should be a df with one row for each distinct combination of levels of factors eg for (M,F) (Y,O) We get M Y M O F Y F O In reality I will have more than 1000 rows so doing by hand not good. Maybe there is a way with "outer", but I couldn't see it. All the best to everybody. Stephen
?expand.grid> expand.grid(c("M","F"),c("Y","O"))Var1 Var2 1 M Y 2 F Y 3 M O 4 F O>Justin On Thu, Nov 3, 2011 at 10:56 AM, Bond, Stephen <Stephen.Bond at cibc.com> wrote:> Greetings useRs, > > What is the easiest way to create a design matrix of several factor variables? Function gendata in Design seems to do that for a fitted model, but how to do that only on several factor vectors?? > > The result should be a df with one row for each distinct combination of levels of factors eg for (M,F) (Y,O) > We get > M Y > M O > F Y > F O > > In reality I will have more than 1000 rows so doing by hand not good. > Maybe there is a way with "outer", but I couldn't see it. > All the best to everybody. > > Stephen > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
?expand.grid is what you're looking for, I think. (which, I agree, is not something one could easily guess) -- Bert On Thu, Nov 3, 2011 at 10:56 AM, Bond, Stephen <Stephen.Bond@cibc.com>wrote:> Greetings useRs, > > What is the easiest way to create a design matrix of several factor > variables? Function gendata in Design seems to do that for a fitted model, > but how to do that only on several factor vectors?? > > The result should be a df with one row for each distinct combination of > levels of factors eg for (M,F) (Y,O) > We get > M Y > M O > F Y > F O > > In reality I will have more than 1000 rows so doing by hand not good. > Maybe there is a way with "outer", but I couldn't see it. > All the best to everybody. > > Stephen > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]