Re: [R] Equivalent of Excel pivot tables in R

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Fri, 25 Apr 2008 19:25:56 -0400

Hi,

Try this:

x="var1 var2 var3

a         x        10
b         y        20
a         z        10
b         z        20
a         z        10
b         z        20"

yourdata=read.table(textConnection(x),header=TRUE) attach(yourdata)

res=tapply(var3,yourdata[,-3],sum) # With tapply! res[is.na(res)]<-0
res
var2
var1 x y z

   a 10 0 20
   b 0 20 40

See also ?tapply.

HTH, Jorge

On Fri, Apr 25, 2008 at 5:54 PM, ppaarrkk <simon_ecc_at_yahoo.co.uk> wrote:

>
> Can somebody tell me how to do the equivalent of a pivot table in R ?
>
>
> For example, if I have :
>
> var1 var2 var3
> a x 10
> b y 20
> a z 10
> b z 20
> a z 10
> b z 20
>
> I could have :
>
> x y z
> a 1 0 2
> b 0 1 2
>
> where entries in the table are counts of var3.
>
> x y z
> a 10 0 20
> b 0 20 40
>
> where entries are sums of var3.
>
>
>
> I would expect it to be tapply(), but I can't see how it would be done.
>
>
> Any suggestions please.
>
>
> --
> View this message in context:
> http://www.nabble.com/Equivalent-of-Excel-pivot-tables-in-R-tp16906289p16906289.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

        [[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. Received on Fri 25 Apr 2008 - 23:28:43 GMT

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 Sat 26 Apr 2008 - 00:30:31 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.

list of date sections of archive