Re: [R] adding group totals to a table

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 10 Dec 2007 11:08:03 -0500

split the data frame, cbind the count and unsplit back:

 DF <- data.frame(A = c("A", "A", "A", "B", "C"), B = c(1, 1, 2, 2, 0))

> g <- paste(DF$A, DF$B)
> s <- split(DF, g)
> u <- lapply(s, function(x) cbind(x, Occurs = nrow(x)))
> unsplit(u, g)

  A B Occurs

1 A 1      2
2 A 1      2
3 A 2      1
4 B 2      1
5 C 0      1

See ?split where there is a very similar example.

On Dec 9, 2007 4:35 AM, christopher snow <snowch_at_coralms.com> wrote:
> I have a table with two columns:
>
> A 1
> A 1
> A 2
> B 2
> C 0
>
> I would like to produce a third column that contains the counts of each
> unique combination of col1 and col2:
>
> A 1 2
> A 1 2
> A 2 1
> B 2 1
> C 0 1
>
> How can I do this in R?
>
> Thanks in advance ...
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
> ______________________________________________
> 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.
>



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 Mon 10 Dec 2007 - 16:33:23 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 Mon 10 Dec 2007 - 17:30:18 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.