# Re: [R] trouble with summary tables with several variables using aggregate function

From: Phil Spector <spector_at_stat.berkeley.edu>
Date: Thu, 19 May 2011 11:10:19 -0700 (PDT)

Luma -

If I understand you correctly, I think the easiest way to get what you want is to use the reshape function on the output from aggregate:

> reshape(Count.Cells,idvar=c('Var1','Var2'),timevar='Var3',direction='wide')

Var1 Var2 dummy.D dummy.I

```1   S1   T1       2       2
2   S2   T1       2       2
3   S1   T2       2       2
7   S2   T2      NA       4

- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector_at_stat.berkeley.edu

```

On Thu, 19 May 2011, Luma R wrote:

> Dear all,
>
> I am having trouble creating summary tables using aggregate function.
>
> given the following table:
>
>
> Var1 Var2 Var3 dummy
> S1 T1 I 1
> S1 T1 I 1
> S1 T1 D 1
> S1 T1 D 1
> S1 T2 I 1
> S1 T2 I 1
> S1 T2 D 1
> S1 T2 D 1
> S2 T1 I 1
> S2 T1 I 1
> S2 T1 D 1
> S2 T1 D 1
> S2 T2 I 1
> S2 T2 I 1
> S2 T2 I 1
> S2 T2 I 1
>
>
> I want to create a summary table that shows for each category of Var1,
> Var2, the number of cells that are Var3=D and Var3-I :
>
> Var1 Var2 Var3(D) Var3(I)
> S1 T1 2 2
> S1 T2 2 2
> S2 T1 2 2
> S2 T2 0 4
>
>
>
> However, if I do: Count.Cells= aggregate(dummy~ Var1+Var2+Var3, FUN='sum')
> , I get:
>
> Var1 Var2 Var3 Count of Resp
> S1 T1 D 2
> S1 T1 I 2
> S1 T2 D 2
> S1 T2 I 2
> S2 T1 D 2
> S2 T1 I 2
> S2 T2 I 4
>
>
> Is there a way to get different columns for each Var3 level?
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

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. Received on Thu 19 May 2011 - 18:13:00 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 19 May 2011 - 18:50:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.