[R] aggregate empty row for pretty appearance also subtotal if possible
jim holtman
jholtman at gmail.com
Fri Oct 3 04:28:14 CEST 2008
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?
More information about the R-help
mailing list