Recently a friend of mine asked me if I could help him collapse his detection history. He want to make 5 days as a single occassion. You might want to do this for vairous reasons. The most important one is that you want to get your detection probability higher. Suppose you detected your species once every 15 days, then you detection probability for a day is going to be very low. So, you might want to have one occassion to have 15 days.
So, if you detection history looks like this
Site 1: 0 0 0 1 0 0 0 0 0 0 1 0
where each 0 or 1 means non-detection or detection in that day and you want to collapse 3 days in one single occassion, you will have the new detection history as
Site 1: 0 1 0 1
Lets see how you can do this is R.
First read the detection history to R.
## reading Detection history
DH <- read.csv("originaldh.csv")
Then we want to get the number of cameratrap days which is given by the number of columns. Here we substract 1 since the data had cameratrap site as its first column.
# number of cameratrap days IN_Occ <- ncol(DH) -1
This is the new occassion length that you want. For this case, my friend wanted one occassion to have five days.
## defining the occassion length OL <- 5 # one occasion is 5 days
What we do next is get the new occassion length. This we do by dividing the total camera trap days by the new occassion length we want. And we ignore the digits after the decimal. For example if the cameratrap days was 17, we would divide it by 5 which would give us 3.4. We would have our new occassion length as 3 then.
## getting the new Occasion length Occ_no <- IN_Occ%/%OL # getting the collapsed occasion length Occ_no N <- nrow(DH) head(DH)
Now we need to create a new matrix for our new detection history. We can use the new occassion length to get the number of columns and previous rows as the new row length because our number of sites don’t change.
## create a new matrix to keep the new DH DH2 <- matrix(nrow = N, ncol = Occ_no)
This is where it gets a little bit tricky. We are basically running a loop to get the maximum number in each three day period. So, if the first three day had detection history 001, the max would be 1. Hence the new detection would be 1. If it was 000, the max would be 0.
# now doing the loop to get the maximum value from within occasion days
for(i in 1:N){
for(j in 1:Occ_no){
DH2[i, j] <- max(DH[i,(((j-1)*OL)+2):((OL*j)+1)]) ## 2 here because the first column is trapsite
}}
Now we are going to make it neat. We give our new matrix column names and rownames. The rowname is going to come from the first column of our original csv file.
## Making it neat
colnames(DH2) <- paste("Occ", as.character(1:Occ_no))
rownames(DH2) <- DH[,1]
DH2
All done. Now you can save your new matrix as a csv file.
# creating a csv file write.csv(DH2, "DH.csv")
You can get the codes as I wrote them here.
If you want to practice it with the same csv file, you can download the csv file here.
Please let me know what you think of it and if you have any suggestions on how to make it better or you have your own way, please let me know.

