Sharma, Dhruv
2008-Oct-02 21:46 UTC
[R] aggregate empty row for pretty appearance also subtotal if possible
Hi, To pretty print aggregates by various dimensions I needed to add a empty row in output of aggregate. For example. d<-(aggregate(data[,cbind("x")], by=list(data$group1,data$group2), sum)) Group.1 Group.2 x 1 A N 3 2 A Y 2 3 B N 420164905 Is there a way to add an empty row between group1 and group 2. So that it looks like Group.1 Group.2 x 1 A N 3 2 A Y 2 3 4 B N 420164905 I need to format a series of aggregates by multi dimensions and I wanted to break the data by empty row between group 1 that people can see some space. Also is there a way to add subtotals by group 1 into the mix : Group.1 Group.2 x 1 A N 3 2 A Y 2 3 5 4 B N 420164905 5 420164905 original data is something like: data Group1 Group2 x A Y 1 B N 1 A Y 1 B N 420164904 A N 3 thanks Dhruv [[alternative HTML version deleted]]
jim holtman
2008-Oct-03 02:28 UTC
[R] aggregate empty row for pretty appearance also subtotal if possible
Here is one way of doing it:> dat <- read.table(textConnection("Group1 Group2 x+ A Y 1 + B N 1 + A Y 1 + B N 420164904 + A N 3"), header=TRUE, as.is=TRUE)> closeAllConnections() > d <- aggregate(dat$x, list(dat$Group1, dat$Group2), sum) > # split on Group.1, and add row with sum > d.l <- lapply(split(d, d$Group.1), function(.df){+ rbind(.df, list('','',sum(.df$x))) + })> do.call(rbind, d.l)Group.1 Group.2 x A.1 A N 3 A.3 A Y 2 A.31 5 B.2 B N 420164905 B.21 420164905 On Thu, Oct 2, 2008 at 5:46 PM, Sharma, Dhruv <Dhruv.Sharma at penfed.org> wrote:> Hi, > To pretty print aggregates by various dimensions I needed to add a > empty row in output of aggregate. > > For example. > > d<-(aggregate(data[,cbind("x")], by=list(data$group1,data$group2), > sum)) > > Group.1 Group.2 x > 1 A N 3 > 2 A Y 2 > 3 B N 420164905 > > Is there a way to add an empty row between group1 and group 2. > So that it looks like > Group.1 Group.2 x > 1 A N 3 > 2 A Y 2 > 3 > 4 B N 420164905 > > > I need to format a series of aggregates by multi dimensions and I wanted > to break the data by empty row between group 1 that people can see some > space. > > Also is there a way to add subtotals by group 1 into the mix : > Group.1 Group.2 x > 1 A N 3 > 2 A Y 2 > 3 5 > 4 B N 420164905 > 5 420164905 > > > original data is something like: > data > Group1 Group2 x > A Y 1 > B N 1 > A Y 1 > B N 420164904 > A N 3 > > > thanks > Dhruv > > [[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
jim holtman
2008-Oct-03 02:38 UTC
[R] aggregate empty row for pretty appearance also subtotal if possible
You can also use the 'reshape' package:> d.m <- melt(dat, measure.var='x') > d.mGroup1 Group2 variable value 1 A Y x 1 2 B N x 1 3 A Y x 1 4 B N x 420164904 5 A N x 3> cast(d.m, Group1+Group2~., sum, margins=TRUE)Group1 Group2 (all) 1 A N 3 2 A Y 2 3 A (all) 5 4 B N 420164905 5 B (all) 420164905 6 (all) (all) 420164910>On Thu, Oct 2, 2008 at 5:46 PM, Sharma, Dhruv <Dhruv.Sharma at penfed.org> wrote:> Hi, > To pretty print aggregates by various dimensions I needed to add a > empty row in output of aggregate. > > For example. > > d<-(aggregate(data[,cbind("x")], by=list(data$group1,data$group2), > sum)) > > Group.1 Group.2 x > 1 A N 3 > 2 A Y 2 > 3 B N 420164905 > > Is there a way to add an empty row between group1 and group 2. > So that it looks like > Group.1 Group.2 x > 1 A N 3 > 2 A Y 2 > 3 > 4 B N 420164905 > > > I need to format a series of aggregates by multi dimensions and I wanted > to break the data by empty row between group 1 that people can see some > space. > > Also is there a way to add subtotals by group 1 into the mix : > Group.1 Group.2 x > 1 A N 3 > 2 A Y 2 > 3 5 > 4 B N 420164905 > 5 420164905 > > > original data is something like: > data > Group1 Group2 x > A Y 1 > B N 1 > A Y 1 > B N 420164904 > A N 3 > > > thanks > Dhruv > > [[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?