HI, Not sure how you wanted the results with the rows having NAs. net1[,sapply(net1,is.factor)]<-lapply(net1[,sapply(net1,is.factor)],as.character) with(net1,aggregate(IND,list(CAMP,LOTE,HAB,TRANS,ORDEN),FUN=sum)) #or with(net1,aggregate(IND,list(CAMP,LOTE,HAB,TRANS,ORDEN),FUN=sum,na.rm=TRUE)) A.K.
________________________________ From: Andrea Goijman <agoij...@cnia.inta.gov.ar> To: arun <smartpink...@yahoo.com> Cc: R help <r-help@r-project.org> Sent: Monday, September 16, 2013 11:42 AM Subject: Re: [R] Aggregate rows with same fields, within factors it works, but it eliminates the rows with NA is there a way to keep those? On Mon, Sep 16, 2013 at 11:22 AM, arun <smartpink...@yahoo.com> wrote: Hi, >Try: > > > aggregate(IND~.,data=net1,sum) > CAMP LOTE HAB TRANS ORDEN IND >1 C1 B1 C C1 0 >2 C1 B1 B B3 ACARI 3 >3 C1 B1 B B1 ARANEAE 1 >4 C1 B1 B B3 ARANEAE 2 >5 C1 B1 B B3 COLEOPTERA 2 >6 C1 B1 B B1 DIPTERA 27 >7 C1 B1 B B3 DIPTERA 11 >8 C1 B1 C C2 DIPTERA 3 >9 C1 B1 B B1 HEMIPTERA 11 >10 C1 B1 B B3 HEMIPTERA 231 >11 C1 B1 C C2 HEMIPTERA 147 >12 C1 B1 B B1 HYMENOPTERA 8 >13 C1 B1 B B3 HYMENOPTERA 2 >14 C1 B1 C C2 HYMENOPTERA 1 >15 C1 B1 B B1 LEPIDOPTERA 1 >16 C1 B1 B B1 NEUROPTERA 1 >17 C1 B1 B B1 ORTHOPTERA 2 >18 C1 B1 B B3 ORTHOPTERA 1 > > >A.K. > > >----- Original Message ----- >From: Andrea Goijman <agoij...@cnia.inta.gov.ar> >To: R help <r-help@r-project.org> >Cc: >Sent: Monday, September 16, 2013 11:09 AM >Subject: [R] Aggregate rows with same fields, within factors > >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]] > >______________________________________________ >R-help@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. > > -- -----Lic. Andrea Paula Goijman, PhD Candidate Grupo Ecología, Biodiversidad y Gestión Ambiental en Agroecosistemas Instituto de Recursos Biológicos CIRN - INTA Castelar, Argentina agoij...@cnia.inta.gov.arhttp://inta.gob.ar/personas/goijman.andrea/ D.B. Warnell School of Forestry and Natural Resources USGS Georgia Cooperative Fish and Wildlife Research Unit University of Georgia Athens, GA 30602 USA Tel. +706.206.4805 Skype: andrea.goijman andre...@uga.edu ______________________________________________ R-help@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.