Hi R, This is a cross tabulation question. Suppose that,> d=read.table("clipboard",header=F)> dV1 V2 V3 A One Apple A One Cake A One Cake B One Apple B One Apple B One Apple> table(d$V2,d$V3)Apple Cake One 4 2 But, I don't want the count to be like the above. Here, it is counting the number of rows in that particular category. What I need is in each category, the count of unique V1's. So, my output should look like, Apple Cake One 2 1 Any ideas please... Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}}
> xV1 V2 V3 1 A One Apple 2 A One Cake 3 A One Cake 4 B One Apple 5 B One Apple 6 B One Apple> tapply(x$V1, list(x$V2, x$V3), function(z) length(unique(z)))Apple Cake One 2 1>On Mon, Sep 29, 2008 at 4:54 AM, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > This is a cross tabulation question. Suppose that, > > > >> d=read.table("clipboard",header=F) > > > >> d > > V1 V2 V3 > > A One Apple > > A One Cake > > A One Cake > > B One Apple > > B One Apple > > B One Apple > > > >> table(d$V2,d$V3) > > > > Apple Cake > > One 4 2 > > > > > > But, I don't want the count to be like the above. Here, it is counting > the number of rows in that particular category. What I need is in each > category, the count of unique V1's. So, my output should look like, > > > > Apple Cake > > One 2 1 > > > > > > Any ideas please... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > 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?
?aggregate or have a look at the reshape package. --- On Mon, 9/29/08, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> From: Shubha Vishwanath Karanth <shubhak at ambaresearch.com> > Subject: [R] Cross-tabulation Question > To: r-help at stat.math.ethz.ch > Received: Monday, September 29, 2008, 4:54 AM > Hi R, > > > > This is a cross tabulation question. Suppose that, > > > > > d=read.table("clipboard",header=F) > > > > > d > > V1 V2 V3 > > A One Apple > > A One Cake > > A One Cake > > B One Apple > > B One Apple > > B One Apple > > > > > table(d$V2,d$V3) > > > > Apple Cake > > One 4 2 > > > > > > But, I don't want the count to be like the above. Here, > it is counting > the number of rows in that particular category. What I need > is in each > category, the count of unique V1's. So, my output > should look like, > > > > Apple Cake > > One 2 1 > > > > > > Any ideas please... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > 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.__________________________________________________________________ [[elided Yahoo spam]]
Try this:> with(unique(x), table(V2, V3))V3 V2 Apple Cake One 2 1 On Mon, Sep 29, 2008 at 4:54 AM, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > This is a cross tabulation question. Suppose that, > > > >> d=read.table("clipboard",header=F) > > > >> d > > V1 V2 V3 > > A One Apple > > A One Cake > > A One Cake > > B One Apple > > B One Apple > > B One Apple > > > >> table(d$V2,d$V3) > > > > Apple Cake > > One 4 2 > > > > > > But, I don't want the count to be like the above. Here, it is counting > the number of rows in that particular category. What I need is in each > category, the count of unique V1's. So, my output should look like, > > > > Apple Cake > > One 2 1 > > > > > > Any ideas please... > > > > Thanks, Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > 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. >