WildCoLab / Introduction-to-Camera-Trap-Data-Management-and-Analysis-in-R

An introduction to analysing standardised camera trap data from the WildCAM network.
Creative Commons Zero v1.0 Universal
22 stars 3 forks source link

Add functionality for customs lengths for the output data frames #17

Open ChrisBeirne opened 9 months ago

ChrisBeirne commented 9 months ago
# Custom day counts - could also do months or years
desired_interval <- "5 days"

# Station / Month / days / Covariates / Species
tmp_bin <- row_lookup
# Simplify the date to monthly
tmp_bin$date <- cut(tmp_bin$date, breaks = seq(min(row_lookup$date),max(row_lookup$date)+1, by= desired_interval))

# Calculate the number of days in each time bin
bin_obs <- tmp_bin %>%
group_by(placename,date ) %>%
summarise(days = n())
# Convert to a data frame
bin_obs <- as.data.frame(bin_obs)

bin_obs[, levels(ind_dat$sp)] <- NA
bin_count <- bin_obs
# For each binth, count the number of individuals/observations
for(i in 1:nrow(bin_obs))
{
# Subset the data and use the same criteria above to cut the dates into the desired interval
tmp <- ind_dat[ind_dat$placename==bin_obs$placename[i] &
cut(as.Date(ind_dat$timestamp), breaks = seq(min(row_lookup$date),max(row_lookup$date)+1, by= desired_interval)) == bin_obs$date[i],]

tmp_stats <- tmp %>% group_by(sp, .drop=F) %>% summarise(obs=n(), count=sum(animal_count))

bin_obs[i,as.character(tmp_stats$sp)] <- tmp_stats$obs
bin_count[i,as.character(tmp_stats$sp)] <- tmp_stats$count

}