I am trying to construct a two-way table where, instead of printing the two-way frequencies in the table, I would like to print the values of a third variable that correspond to the frequencies. For example, the following is easily constructed in R> fact1 <- factor(sample(LETTERS[1:3],10,replace=TRUE))> fact2 <- factor(sample(LETTERS[25:26],10,replace=TRUE))> fact3 <- letters[1:10]> data.frame(fact1,fact2,fact3)fact1 fact2 fact3 1 C Z a 2 A Y b 3 A Y c 4 C Z d 5 A Z e 6 A Y f 7 B Y g 8 B Y h 9 C Z i 10 A Y j> table(fact1,fact2)fact2 fact1 Y Z A 4 1 B 2 0 C 0 3 But I would like to create something like this (done physically by hand) ... fact2 fact1 Y Z A b,c,f,j e B g,h - C - a,d,i Any help would be appreciated. Thank you in advance. For what it is worth,> Sys.info()sysname "Windows" release "XP" version "build 2600, Service Pack 2" [[alternative HTML version deleted]]
If I understood propelly > tapply(fact3, list(fact1, fact2) , paste, collapse = ",") A. Derek Ogle wrote:> I am trying to construct a two-way table where, instead of printing the > two-way frequencies in the table, I would like to print the values of a > third variable that correspond to the frequencies. > > > > For example, the following is easily constructed in R > > > > >> fact1 <- factor(sample(LETTERS[1:3],10,replace=TRUE)) >> > > >> fact2 <- factor(sample(LETTERS[25:26],10,replace=TRUE)) >> > > >> fact3 <- letters[1:10] >> > > >> data.frame(fact1,fact2,fact3) >> > > fact1 fact2 fact3 > > 1 C Z a > > 2 A Y b > > 3 A Y c > > 4 C Z d > > 5 A Z e > > 6 A Y f > > 7 B Y g > > 8 B Y h > > 9 C Z i > > 10 A Y j > > >> table(fact1,fact2) >> > > fact2 > > fact1 Y Z > > A 4 1 > > B 2 0 > > C 0 3 > > > > But I would like to create something like this (done physically by hand) > ... > > > > fact2 > > fact1 Y Z > > A b,c,f,j e > > B g,h - > > C - a,d,i > > > > Any help would be appreciated. Thank you in advance. > > > > For what it is worth, > > > > >> Sys.info() >> > > sysname > > "Windows" > > release > > "XP" > > version > > "build 2600, Service Pack 2" > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >
Yes, that was exactly what I was looking for. Very concise. Thank you. My "real" example has many more items in each cell. I will now have to figure out how to put a "carriage return" after, say, every fourth item. Nevertheless, thank you very much for the quick and accurate answer. -----Original Message----- From: r at quantide.com [mailto:r at quantide.com] Sent: Friday, January 23, 2009 9:25 AM To: Derek Ogle Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Table Modification If I understood propelly > tapply(fact3, list(fact1, fact2) , paste, collapse = ",") A.
?strwrap On Fri, Jan 23, 2009 at 10:47 AM, Derek Ogle <DOgle at northland.edu> wrote:> Yes, that was exactly what I was looking for. Very concise. Thank you. > > My "real" example has many more items in each cell. I will now have to > figure out how to put a "carriage return" after, say, every fourth item. > > Nevertheless, thank you very much for the quick and accurate answer. > > -----Original Message----- > From: r at quantide.com [mailto:r at quantide.com] > Sent: Friday, January 23, 2009 9:25 AM > To: Derek Ogle > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Table Modification > > If I understood propelly > > > tapply(fact3, list(fact1, fact2) , paste, collapse = ",") > > A. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Maybe Matching Threads
- Fwd: Enduring LME confusion… or Psychologists and Mixed-Effects
- Enduring LME confusion… or Psychologists and Mixed-Effects
- Reconstruction of a "valid" expression within a function
- Re: R-help Digest, Vol 18, Issue 12
- Rearranging long tables, Sweave, xtable, LaTeX