Hi, I have a data frame with two factors (well, more, but 2 for simple consideration), and I want to display the different combinations of the them that actually occur in the data. In reality, there are too many of them to do to do a 'table' call and have one col vertical and one col horizontal (I don't want any of the factors listed horizontally). Before I try to write a function to do this for me, I was wondering if there were alternate printing styles for data that already exist, and if someone could direct me to them? Inclded is a sample code and 2 possibilities (others welcome for consideration) of how I want to display some data. Thanks, Matt ----- df <- data.frame( f1=rep(c("Maj I", "Maj II", "Maj III"), each=3), f2=c("Minor A", "Minor A", "Minor A", "Minor A", "Minor B", "Minor B", "Minor B", "Minor C", "Minor C") ) ----- What I want printed is something like: ------------------- f1 f2 Maj I Minor A Maj II Minor A Minor B Maj III Minor B Minor C ------------------- or ------------------- f1 f2 Maj I Minor A Maj II Minor A Maj II Minor B Maj III Minor B Maj III Minor C
Jan van der Laan
2010-Oct-29 14:08 UTC
[R] Printing data.frame data: alternatives to print?
Matt, Below are three (of the probably many more) possible ways of doing this: aggregate(1:nrow(df), df, length) ftable(1 ~ f1 + f2, data=df) library(plyr) ddply(df, .(f1,f2), nrow) Regards, Jan On 29-10-2010 15:53, Matthew Pettis wrote:> Hi, > > I have a data frame with two factors (well, more, but 2 for simple > consideration), and I want to display the different combinations of > the them that actually occur in the data. In reality, there are too > many of them to do to do a 'table' call and have one col vertical and > one col horizontal (I don't want any of the factors listed > horizontally). Before I try to write a function to do this for me, I > was wondering if there were alternate printing styles for data that > already exist, and if someone could direct me to them? Inclded is a > sample code and 2 possibilities (others welcome for consideration) of > how I want to display some data. > > Thanks, > Matt > > ----- > > df<- data.frame( > f1=rep(c("Maj I", "Maj II", "Maj III"), each=3), > f2=c("Minor A", "Minor A", "Minor A", "Minor A", "Minor B", "Minor > B", "Minor B", "Minor C", "Minor C") > ) > > ----- > > What I want printed is something like: > > ------------------- > f1 f2 > Maj I Minor A > > Maj II Minor A > Minor B > > Maj III Minor B > Minor C > ------------------- > > or > > ------------------- > f1 f2 > Maj I Minor A > > Maj II Minor A > Maj II Minor B > > Maj III Minor B > Maj III Minor C > > ______________________________________________ > 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.
Is this what you want:> dff1 f2 1 Maj I Minor A 2 Maj I Minor A 3 Maj I Minor A 4 Maj II Minor A 5 Maj II Minor B 6 Maj II Minor B 7 Maj III Minor B 8 Maj III Minor C 9 Maj III Minor C> df[!duplicated(df),]f1 f2 1 Maj I Minor A 4 Maj II Minor A 5 Maj II Minor B 7 Maj III Minor B 8 Maj III Minor C>On Fri, Oct 29, 2010 at 9:53 AM, Matthew Pettis <matthew.pettis at gmail.com> wrote:> Hi, > > I have a data frame with two factors (well, more, but 2 for simple > consideration), and I want to display the different combinations of > the them that actually occur in the data. ?In reality, there are too > many of them to do to do a 'table' call and have one col vertical and > one col horizontal (I don't want any of the factors listed > horizontally). ?Before I try to write a function to do this for me, I > was wondering if there were alternate printing styles for data that > already exist, and if someone could direct me to them? ?Inclded is a > sample code and 2 possibilities (others welcome for consideration) of > how I want to display some data. > > Thanks, > Matt > > ----- > > df <- data.frame( > ? ?f1=rep(c("Maj I", "Maj II", "Maj III"), each=3), > ? ?f2=c("Minor A", "Minor A", "Minor A", "Minor A", "Minor B", "Minor > B", "Minor B", "Minor C", "Minor C") > ) > > ----- > > What I want printed is something like: > > ------------------- > ? ? ?f1 ? ? ? ? f2 > Maj I ? ? ? Minor A > > Maj II ? ? ?Minor A > ? ? ? ? ? ?Minor B > > Maj III ? ? Minor B > ? ? ? ? ? ?Minor C > ------------------- > > or > > ------------------- > ? ? ?f1 ? ? ? ? f2 > Maj I ? ? ? Minor A > > Maj II ? ? ?Minor A > Maj II ? ? ?Minor B > > Maj III ? ? Minor B > Maj III ? ? Minor C > > ______________________________________________ > 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?