Re: [R] rep for multiple categories

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Mon, 28 Mar 2011 16:59:44 -0300

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

______________________________________________
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 28 Mar 2011 - 20:02:16 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 28 Mar 2011 - 20:10:25 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.

list of date sections of archive