Dear R list,

I want to aggregate the number of individuals 'IND' of the same ORDER,
within each site and season CAMP,TRANS... but I also want to keep record of
the habitat HAB and LOTE

For example I have this:

         CAMP LOTE HAB TRANS IND       ORDEN
1765   C1   B1   B    B1   7   HEMIPTERA
1766   C1   B1   B    B1   7     DIPTERA
1767   C1   B1   B    B1   1     DIPTERA
1768   C1   B1   B    B1   1  NEUROPTERA
1769   C1   B1   B    B1   1   HEMIPTERA
1770   C1   B1   B    B1   5     DIPTERA
1771   C1   B1   B    B1   1     DIPTERA

And I want this

          CAMP LOTE HAB TRANS IND       ORDEN
1765   C1   B1   B    B1   8   HEMIPTERA
1766   C1   B1   B    B1   14     DIPTERA
1768   C1   B1   B    B1   1  NEUROPTERA


I'm using aggregate the way I show below, but it is not working, and I
cannot figure out why.

Thanks!

Andrea



net1<-structure(list(CAMP = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("C1",
"C2", "C3", "C4"), class = "factor"), LOTE = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("B1", "B4", "B5", "F7", "G6", "G8", "R10", "W9",
"Z2", "Z3"), class = "factor"), HAB = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label =
c("B",
"C"), class = "factor"), TRANS = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("B1",
"B2", "B3", "C1", "C2", "C3"), class = "factor"), IND = c(2L,
6L, 7L, 1L, 1L, 7L, 7L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 4L, 1L, 2L,
1L, 1L, NA, NA, NA, NA, 28L, 4L, 2L, 1L, 3L, 193L, 1L, 2L, 7L,
2L, 1L, 5L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 62L, 1L, 1L, 1L, 80L,
1L, 1L, 4L), ORDEN = structure(c(9L, 10L, 8L, 8L, 15L, 9L, 8L,
8L, 12L, 9L, 8L, 8L, 11L, 3L, 8L, 8L, 10L, 9L, 15L, 1L, 1L, 1L,
1L, 9L, 8L, 8L, 5L, 2L, 9L, 10L, 3L, 9L, 9L, 9L, 8L, 10L, 5L,
15L, 1L, 1L, 1L, 1L, 9L, 8L, 10L, 9L, 9L, 8L, 8L, 9L), .Label = c("",
"ACARI", "ARANEAE", "CHILOGNATHA", "COLEOPTERA", "DERMAPTERA",
"DICTYOPTERA", "DIPTERA", "HEMIPTERA", "HYMENOPTERA", "LEPIDOPTERA",
"NEUROPTERA", "NN", "ODONATA", "ORTHOPTERA", "PSOCOPTERA", "STREPSIPTERA",
"THYSANOPTERA", "TRICHOPTERA"), class = "factor")), .Names = c("CAMP",
"LOTE", "HAB", "TRANS", "IND", "ORDEN"), row.names = c(1760L,
1761L, 1762L, 1763L, 1764L, 1765L, 1766L, 1767L, 1768L, 1769L,
1770L, 1771L, 1772L, 1773L, 1920L, 1921L, 1922L, 1923L, 1924L,
1774L, 1775L, 1776L, 1777L, 1778L, 1779L, 1780L, 1781L, 1782L,
1783L, 1784L, 1785L, 1786L, 1787L, 1788L, 1789L, 1790L, 1791L,
1925L, 1731L, 1732L, 1733L, 1734L, 1735L, 1736L, 1737L, 1738L,
1739L, 1740L, 1741L, 1742L), class = "data.frame")

#generate grouping list
b <- list(net1$CAMP, net1$LOTE, net1$HAB, net1$TRANS, net1$ORDEN)

#aggregate data
net2 <- aggregate(x = net1, by =(b), FUN = "sum")

	[[alternative HTML version deleted]]

