Hi useRs! I would like to know how to make aggregated data.frame with aggregate() tabulated. For example, I run the following command to aggregate re with respect to group1 and group2. > (aggr <- with(final, aggregate(re, group1, group2, mean))) Group.1 Group.2 x 1 1992 15 0.16392 2 1993 15 0.15467 3 1994 15 0.15456 4 1995 15 0.15391 5 1996 15 0.16511 6 1997 15 0.17368 7 1998 15 0.17955 8 1999 15 0.19805 9 2000 15 0.20546 10 1992 16 0.16237 11 1993 16 0.18359 12 1994 16 0.13811 13 1995 16 0.13988 14 1996 16 0.15604 15 1997 16 0.16802 16 1998 16 0.16978 17 1999 16 0.18407 18 2000 16 0.18646 19 1992 17 0.24604 20 1993 17 0.25263 21 1994 17 0.21407 22 1995 17 0.26483 23 1996 17 0.26427 24 1997 17 0.26282 25 1998 17 0.31137 26 1999 17 0.26982 27 2000 17 0.22898 28 1992 18 0.29417 29 1993 18 0.36586 30 1994 18 0.25143 31 1995 18 0.25570 32 1996 18 0.24628 33 1997 18 0.24293 34 1998 18 0.24888 35 1999 18 0.19515 36 2000 18 0.18057 I want make aggr as follows with class of data.frame. 1992 1993 1994 1995 1996 1997 1998 1999 2000 15 0.16392 0.15467...... 16 0.16237 0.18359....... 17 ..... 18 ...... Thank you in advance.
Hi there, Try this: R> tapply(aggr$x,aggr[,c(2,1)],function(x) x) Group.1 Group.2 1992 1993 1994 1995 1996 1997 1998 1999 2000 15 0.16392 0.15467 0.15456 0.15391 0.16511 0.17368 0.17955 0.19805 0.20546 16 0.16237 0.18359 0.13811 0.13988 0.15604 0.16802 0.16978 0.18407 0.18646 17 0.24604 0.25263 0.21407 0.26483 0.26427 0.26282 0.31137 0.26982 0.22898 18 0.29417 0.36586 0.25143 0.25570 0.24628 0.24293 0.24888 0.19515 0.18057 HTH, Jorge On Fri, May 9, 2008 at 12:25 PM, Oh Dong-hyun <r.arecibo@gmail.com> wrote:> Hi useRs! > > I would like to know how to make aggregated data.frame with aggregate() > tabulated. > > For example, I run the following command to aggregate re with respect to > group1 and group2. > > > (aggr <- with(final, aggregate(re, group1, group2, mean))) > Group.1 Group.2 x > 1 1992 15 0.16392 > 2 1993 15 0.15467 > 3 1994 15 0.15456 > 4 1995 15 0.15391 > 5 1996 15 0.16511 > 6 1997 15 0.17368 > 7 1998 15 0.17955 > 8 1999 15 0.19805 > 9 2000 15 0.20546 > 10 1992 16 0.16237 > 11 1993 16 0.18359 > 12 1994 16 0.13811 > 13 1995 16 0.13988 > 14 1996 16 0.15604 > 15 1997 16 0.16802 > 16 1998 16 0.16978 > 17 1999 16 0.18407 > 18 2000 16 0.18646 > 19 1992 17 0.24604 > 20 1993 17 0.25263 > 21 1994 17 0.21407 > 22 1995 17 0.26483 > 23 1996 17 0.26427 > 24 1997 17 0.26282 > 25 1998 17 0.31137 > 26 1999 17 0.26982 > 27 2000 17 0.22898 > 28 1992 18 0.29417 > 29 1993 18 0.36586 > 30 1994 18 0.25143 > 31 1995 18 0.25570 > 32 1996 18 0.24628 > 33 1997 18 0.24293 > 34 1998 18 0.24888 > 35 1999 18 0.19515 > 36 2000 18 0.18057 > > I want make aggr as follows with class of data.frame. > 1992 1993 1994 1995 1996 1997 1998 1999 > 2000 > 15 0.16392 0.15467...... > 16 0.16237 0.18359....... > 17 ..... > 18 ...... > > Thank you in advance. > > ______________________________________________ > R-help@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]]
Try: xtabs(x ~ Group.2 + Group.1, data=x) On Fri, May 9, 2008 at 1:25 PM, Oh Dong-hyun <r.arecibo@gmail.com> wrote:> Hi useRs! > > I would like to know how to make aggregated data.frame with aggregate() > tabulated. > > For example, I run the following command to aggregate re with respect to > group1 and group2. > > > (aggr <- with(final, aggregate(re, group1, group2, mean))) > Group.1 Group.2 x > 1 1992 15 0.16392 > 2 1993 15 0.15467 > 3 1994 15 0.15456 > 4 1995 15 0.15391 > 5 1996 15 0.16511 > 6 1997 15 0.17368 > 7 1998 15 0.17955 > 8 1999 15 0.19805 > 9 2000 15 0.20546 > 10 1992 16 0.16237 > 11 1993 16 0.18359 > 12 1994 16 0.13811 > 13 1995 16 0.13988 > 14 1996 16 0.15604 > 15 1997 16 0.16802 > 16 1998 16 0.16978 > 17 1999 16 0.18407 > 18 2000 16 0.18646 > 19 1992 17 0.24604 > 20 1993 17 0.25263 > 21 1994 17 0.21407 > 22 1995 17 0.26483 > 23 1996 17 0.26427 > 24 1997 17 0.26282 > 25 1998 17 0.31137 > 26 1999 17 0.26982 > 27 2000 17 0.22898 > 28 1992 18 0.29417 > 29 1993 18 0.36586 > 30 1994 18 0.25143 > 31 1995 18 0.25570 > 32 1996 18 0.24628 > 33 1997 18 0.24293 > 34 1998 18 0.24888 > 35 1999 18 0.19515 > 36 2000 18 0.18057 > > I want make aggr as follows with class of data.frame. > 1992 1993 1994 1995 1996 1997 1998 1999 > 2000 > 15 0.16392 0.15467...... > 16 0.16237 0.18359....... > 17 ..... > 18 ...... > > Thank you in advance. > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]