Hi folks, Suppose I have a series of cases each with categorical factors A, B. What is the best way to "glue" A and B together into a single factor? For example, given A0 B1 ... A1 B1 ... A0 B2 ... A1 B0 ... A0 B0 ... A1 B2 ... then I'd like to end up with a single factor with levels A0B0, A0B1, A0B2, A1B0, A1B1, A1B2 according to all the combinations which actually occur in the data (e.g. if (A1,B2) did not occur in the data, then A1B2 would not be a level in the resulting "glued" factor). I can see clumsy and tedious ways involving manipulating strings, paste(), etc.; but I'm sure it must be possible more elegantly. With thanks, and best wishes to all, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 15-Jun-04 Time: 09:26:06 ------------------------------ XFMail ------------------------------
(Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> writes: ...> then I'd like to end up with a single factor with levels > > A0B0, A0B1, A0B2, A1B0, A1B1, A1B2 > > according to all the combinations which actually occur in the > data (e.g. if (A1,B2) did not occur in the data, then A1B2 would > not be a level in the resulting "glued" factor). > > I can see clumsy and tedious ways involving manipulating strings, > paste(), etc.; but I'm sure it must be possible more elegantly.You mean like interaction()? -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Dear Ted, you could try something like, dat <- data.frame(V1=sample(LETTERS[1:3], 10, rep=T), V2=sample(letters[1:3], 10, rep=T)) mat <- apply(as.matrix(dat), 1, function(x) paste(x, collapse="")) dat$glue <- factor(mat) dat I hope this helps. Best, Dimitris ---- Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Ted Harding" <Ted.Harding at nessie.mcc.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, June 15, 2004 10:26 AM Subject: [R] "Glueing" factors together> Hi folks, > > Suppose I have a series of cases each with categorical > factors A, B. > > What is the best way to "glue" A and B together into a single > factor? For example, given > > A0 B1 ... > A1 B1 ... > A0 B2 ... > A1 B0 ... > A0 B0 ... > A1 B2 ... > > then I'd like to end up with a single factor with levels > > A0B0, A0B1, A0B2, A1B0, A1B1, A1B2 > > according to all the combinations which actually occur in the > data (e.g. if (A1,B2) did not occur in the data, then A1B2 would > not be a level in the resulting "glued" factor). > > I can see clumsy and tedious ways involving manipulating strings, > paste(), etc.; but I'm sure it must be possible more elegantly. > > With thanks, and best wishes to all, > Ted. > > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> > Fax-to-email: +44 (0)870 167 1972 > Date: 15-Jun-04 Time: 09:26:06 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
Hi, Ted: Probably not the most elegant way, but not too tedious either. >a <- as.factor(c("A0","A1","A2")) >b <- as.factor(c("B0","B1","B2")) > as.vector(outer(a,b,FUN=paste,sep="")) [1] "A0B0" "A1B0" "A2B0" "A0B1" "A1B1" "A2B1" "A0B2" "A1B2" "A2B2" Best, ft. -- Fernando TUSELL e-mail: Departamento de Econometr??a y Estad??stica etptupaf at bs.ehu.es Facultad de CC.EE. y Empresariales Tel: (+34)94.601.3733 Avenida Lendakari Aguirre, 83 Fax: (+34)94.601.3754 E-48015 BILBAO (Spain) Secr: (+34)94.601.3740
Apologies, disregard my former message! I overlloked the requirement that only combinations present in the data must be taken. ft. -- Fernando TUSELL e-mail: Departamento de Econometr??a y Estad??stica etptupaf at bs.ehu.es Facultad de CC.EE. y Empresariales Tel: (+34)94.601.3733 Avenida Lendakari Aguirre, 83 Fax: (+34)94.601.3754 E-48015 BILBAO (Spain) Secr: (+34)94.601.3740