Hi, I am R beginner and am trying to figure out how to generate a complete list of species for every point, visit, and year. The code below is close but does not give me a list of species for every point, visit, and year in my data set. spplist<-unique(sumPtCt$Species) spplength<-length(spplist) Pointlist<-unique(sumPtCt$Point) Pointlength<-length(Pointlist) Visitlist<-unique(sumPtCt$Visit) Visitlength<-length(Visitlist) Yearlist<-unique(sumPtCt$Year) Yearlength<-length(Yearlist) s<-rep(spplist, each=Pointlength, Visitlength, Yearlength) p<-rep(Pointlist, spplength) v<-rep(Visitlist, spplength) y<-rep(Yearlist, spplength) template<-data.frame(Species=s,Point=p, Visit=v, Year=y) ###merge template and data and replace NAs with 0 FinalPtCt<-merge(template, sumPtCt, all=T) FinalPtCt$Number[is.na(FinalPtCt$Number)]<-0 Essentially I have data that look like this SPP Point Visit Number BUFF 1 1 5 WEGR 1 1 10 CLGR 1 1 15 WEGU 2 1 5 RUDU 2 1 15 HOGR 2 1 5 But I want to generate this Spp Point Visit Number BUFF 1 1 5 WEGR 1 1 10 CLGR 1 1 15 WEGU 1 1 0 RUDU 1 1 0 HOGR 1 1 0 WEGU 2 1 5 RUDU 2 1 15 HOGR 2 1 5 BUFF 2 1 0 WEGR 2 1 0 CLGR 2 1 0
Try this: as.data.frame.table(xtabs(Number ~ SPP + Point + Visit, template)) On Mon, Mar 28, 2011 at 3:43 PM, BORGMANN,Kathi <kborgmann at audubon.org> wrote:> Hi, > I am R beginner and am trying to figure out how to generate a complete list of species for every point, visit, and year. The code below is close but does not give me a list of species for every point, visit, and year in my data set. > > spplist<-unique(sumPtCt$Species) > spplength<-length(spplist) > Pointlist<-unique(sumPtCt$Point) > Pointlength<-length(Pointlist) > Visitlist<-unique(sumPtCt$Visit) > Visitlength<-length(Visitlist) > Yearlist<-unique(sumPtCt$Year) > Yearlength<-length(Yearlist) > s<-rep(spplist, each=Pointlength, Visitlength, Yearlength) > p<-rep(Pointlist, spplength) > v<-rep(Visitlist, spplength) > y<-rep(Yearlist, spplength) > template<-data.frame(Species=s,Point=p, Visit=v, Year=y) > > ###merge template and data and replace NAs with 0 > FinalPtCt<-merge(template, sumPtCt, all=T) > FinalPtCt$Number[is.na(FinalPtCt$Number)]<-0 > > > Essentially I have data that look like this > ?SPP ?Point ?Visit ?Number > BUFF ? 1 ? ? ?1 ? ? ? 5 > WEGR ? 1 ? ? ?1 ? ? ? 10 > CLGR ? 1 ? ? ?1 ? ? ? 15 > WEGU ? 2 ? ? ?1 ? ? ? ?5 > RUDU ? 2 ? ? ?1 ? ? ? 15 > HOGR ? 2 ? ? ?1 ? ? ? ?5 > > > But I want to generate this > Spp ?Point ? ? ? ? ? ? ?Visit ? ? ? ? ? ? ? ? ? Number > BUFF ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 5 > WEGR ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 10 > CLGR ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 15 > WEGU ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 0 > RUDU ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 0 > HOGR ?1 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 0 > WEGU ?2 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 5 > RUDU ?2 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 15 > HOGR ?2 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 5 > BUFF ?2 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 0 > WEGR ?2 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 0 > CLGR ?2 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 0 > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Hi Kathi, Are you looking for something like this?> Alist <- letters[1:4] > Blist <- 1:5 > Clist <- LETTERS[24:26] > Alist[1] "a" "b" "c" "d"> Blist[1] 1 2 3 4 5> Clist[1] "X" "Y" "Z"> expand.grid(Alist, Blist, Clist)Var1 Var2 Var3 1 a 1 X 2 b 1 X 3 c 1 X 4 d 1 X 5 a 2 X 6 b 2 X 7 c 2 X 8 d 2 X 9 a 3 X 10 b 3 X 11 c 3 X 12 d 3 X 13 a 4 X 14 b 4 X 15 c 4 X 16 d 4 X 17 a 5 X 18 b 5 X 19 c 5 X 20 d 5 X 21 a 1 Y 22 b 1 Y 23 c 1 Y 24 d 1 Y 25 a 2 Y 26 b 2 Y 27 c 2 Y 28 d 2 Y 29 a 3 Y 30 b 3 Y 31 c 3 Y 32 d 3 Y 33 a 4 Y 34 b 4 Y 35 c 4 Y 36 d 4 Y 37 a 5 Y 38 b 5 Y 39 c 5 Y 40 d 5 Y 41 a 1 Z 42 b 1 Z 43 c 1 Z 44 d 1 Z 45 a 2 Z 46 b 2 Z 47 c 2 Z 48 d 2 Z 49 a 3 Z 50 b 3 Z 51 c 3 Z 52 d 3 Z 53 a 4 Z 54 b 4 Z 55 c 4 Z 56 d 4 Z 57 a 5 Z 58 b 5 Z 59 c 5 Z 60 d 5 Z Steven McKinney ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of BORGMANN,Kathi [kborgmann at audubon.org] Sent: March 28, 2011 11:43 AM To: r-help at r-project.org Subject: [R] rep for multiple categories Hi, I am R beginner and am trying to figure out how to generate a complete list of species for every point, visit, and year. The code below is close but does not give me a list of species for every point, visit, and year in my data set. spplist<-unique(sumPtCt$Species) spplength<-length(spplist) Pointlist<-unique(sumPtCt$Point) Pointlength<-length(Pointlist) Visitlist<-unique(sumPtCt$Visit) Visitlength<-length(Visitlist) Yearlist<-unique(sumPtCt$Year) Yearlength<-length(Yearlist) s<-rep(spplist, each=Pointlength, Visitlength, Yearlength) p<-rep(Pointlist, spplength) v<-rep(Visitlist, spplength) y<-rep(Yearlist, spplength) template<-data.frame(Species=s,Point=p, Visit=v, Year=y) ###merge template and data and replace NAs with 0 FinalPtCt<-merge(template, sumPtCt, all=T) FinalPtCt$Number[is.na(FinalPtCt$Number)]<-0 Essentially I have data that look like this SPP Point Visit Number BUFF 1 1 5 WEGR 1 1 10 CLGR 1 1 15 WEGU 2 1 5 RUDU 2 1 15 HOGR 2 1 5 But I want to generate this Spp Point Visit Number BUFF 1 1 5 WEGR 1 1 10 CLGR 1 1 15 WEGU 1 1 0 RUDU 1 1 0 HOGR 1 1 0 WEGU 2 1 5 RUDU 2 1 15 HOGR 2 1 5 BUFF 2 1 0 WEGR 2 1 0 CLGR 2 1 0 ______________________________________________ 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.