[R] aggregate help

Sam Steingold sds at gnu.org
Thu Sep 20 20:06:40 CEST 2012


I want to count attributes of IDs:
--8<---------------cut here---------------start------------->8---
z <- data.frame(id=c(10,20,10,30,10,20),
                a1=c("a","b","a","c","b","b"),
                a2=c("x","y","x","z","z","y"),
                stringsAsFactors=FALSE)
> z
  id a1 a2
1 10  a  x
2 20  b  y
3 10  a  x
4 30  c  z
5 10  b  z
6 20  b  y
--8<---------------cut here---------------end--------------->8---
I want to get something like
--8<---------------cut here---------------start------------->8---
id a1.tot a1.val1 a1.num1 a1.val2 a1.num2 a2.tot a2.val1 a2.num1 a2.val2 a2.num2
10   3     "a"      2      "b"      1       3      "x"     2       "z"     1
20   2     "b"      2      <NA>     0       2      "y"     2       <NA>    0
30   1     "c"      1      <NA>     0       1      "z"     1       <NA>    0
--8<---------------cut here---------------end--------------->8---
(except that I don't care what appears in the cells marked with <NA>)
I tried this:
--8<---------------cut here---------------start------------->8---
aggregate(z,by=list(id=z$id),function (s) {
  t <- sort(table(s),decreasing=TRUE)
  if (length(t) == 1)
    list(length(s),names(t)[1],t[1],"junk",0)
  else
    list(length(s),names(t)[1],t[1],names(t)[2],t[2])
 })
  id id a1 a2
1 10  3  3  3
2 20  2  2  2
3 30  1  1  1
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs
--8<---------------cut here---------------end--------------->8---
Thanks!
-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://www.memritv.org http://palestinefacts.org
http://pmw.org.il http://dhimmi.com http://jihadwatch.org http://ffii.org
I'm out of my mind, but feel free to leave a message...



More information about the R-help mailing list