[R] Cross-tabulation Question

jim holtman jholtman at gmail.com
Mon Sep 29 14:38:49 CEST 2008


> x
  V1  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?



More information about the R-help mailing list