I don't know of any generic code that does what you want. I have thought about writing something like that but it depends on the data/model structure. If you know R, then this is done fairly easily once you know a few tricks. If your data are in dataframe x and it includes both the initial release and recaptures then conceptually it is something like the following where ID is the animal ID and occasions are years (Year) and each is a factor variable with the same levels. If ID and Year are numeric in your data do the following first.
- Code: Select all
allyears=sort(unique(x$Year))
allids=sort(unique(x$ID))
x$Year=factor(x$Year)
z$Year=factor(z$Year,levels=allyears)
x$ID=factor(x$ID)
z$ID=factor(z$ID,levels=allids)
Doing so will mean that when you use the table function you will get the same number of rows and columns in each table.
After doing that create the release/recapture table
- Code: Select all
freq=table(x$ID,x$Year)
freq[freq>0]=1
Then if it was only recaptures you would simply do
- Code: Select all
ch=apply(freq,1,paste,collapse="")
and ch will be the encounter history string.
If you also had dead encounters and they are in dataframe z then you can add them with
- Code: Select all
dfreq=table(z$ID,z$Year)
dfreq[dfreq>0]=2
Note that I'm also assuming that you have at least one release or recapture per year. If you don't then you'll either need to set time interval values for the gap or specify range of years in level for factor. Anyhow, hopefully you can start to appreciate why writing a generic routine is not easy. But if you both Year and ID are factors with same levels as described above then using the following will construct what you want.
- Code: Select all
chmat=matrix(0,nrow=nrow(freq),ncol=2*ncol(freq))
chmat[,seq(1,ncol(chmat))]=freq
chmat[,seq(2,ncol(chmat))]=dfreq
ch=apply(chmat,1,paste,collapse="")
Putting this altogether use:
- Code: Select all
allyears=sort(unique(x$Year))
allids=sort(unique(x$ID))
x$Year=factor(x$Year)
z$Year=factor(z$Year,levels=allyears)
x$ID=factor(x$ID)
z$ID=factor(z$ID,levels=allids)
freq=table(x$ID,x$Year)
freq[freq>0]=1
dfreq=table(z$ID,z$Year)
dfreq[dfreq>0]=2
chmat=matrix(0,nrow=nrow(freq),ncol=2*ncol(freq))
chmat[,seq(1,ncol(chmat),2)]=freq
chmat[,seq(2,ncol(chmat),2)]=dfreq
ch=apply(chmat,1,paste,collapse="")
Here is an example
- Code: Select all
x=data.frame(ID=c(1,1,2,3,3),Year=c(1990,1991,1990,1991,1992))
z=data.frame(ID=c(2),Year=1990)
With this result
> ch
[1] "101000" "120000" "001010"
Obviously I have used a simple example and there may be complications like non-annual occasions, unequal intervals etc. But hopefully this will help you get started.
You can do the same thing in Excel with pivot table which is what I did before I learned R. It is too easy to make an unintended error in Excel that you may not see. Granted it is also possible to make an error in the R code but it is usually obvious and regardless it is a lot easier to fix and repeat than all that pointing and clicking.
It is real easy to convince yourself that you are only doing this once and I can probably get it done faster in Excel than in R. I've been there done that. Don't do it. Recently I had a colleague send me a file in Excel and asked me to do some manipulations. It was a one time thing so I thought why not just do it in Excel and then he could relate to it. Six hours later I couldn't work out how to do it and I'm pretty good at Excel and used to program in VBA. I finally smartly (or not so since it took me 6 hours) gave up and did it in R with 10 lines of code in about 15 minutes. I now have a sticky on my portable to remind me. It says "Don't do data analysis in Excel!"
regards --jeff