rm(list = ls()) setwd("") #set your working directory library(foreign) library(data.table) library(reshape2) library(stringr) library(ggplot2) BBS <- rbindlist(lapply(list.files("",full.names=TRUE),read.csv)) #this needs to have the full path name to your folder of all the BBS CSV files BBS$rteno <- paste(BBS$statenum,formatC(BBS$Route, width=3, flag="0"),sep="") speciesnames <- read.csv("SpeciesList.csv") routeinfo <- read.csv("weather.csv") routeinfo$rteno <- paste(routeinfo$statenum,formatC(routeinfo$Route, width=3, flag="0"),sep="") routeinfo <- routeinfo[c("RouteDataId","Year","ObsN","rteno","EndWind","StartWind","RunType","RPID")] routeinfo$firstyear <- c(1L,routeinfo$ObsN[-1]!=routeinfo$ObsN[-nrow(routeinfo)]) BBS$countrynum <- NULL BBS$rteno <- paste(BBS$statenum,formatC(BBS$Route, width=3, flag="0"),sep="") BBS$StopTotal <- NULL BBS <- merge(BBS,routeinfo,by=c("rteno","Year")) route_data <- read.csv("routes.csv") route_data$rteno <- paste(route_data$statenum,formatC(route_data$Route, width=3, flag="0"),sep="") route_data <- route_data[c("BCR","rteno")] BBS <- merge(BBS,route_data,by="rteno") #if you only want to keep data from a certain BCR... #BBS <- BBS[BBS$BCR %in% c(), ] vcf <- read.dbf("landcover/bbs_buffers.dbf") vcf <- vcf[,c("rteno",grep("[[:digit:]]", names(vcf), perl=TRUE, value=TRUE))] vcf <- melt(vcf,id=c("rteno","interval")) vcf$variable <- as.character(vcf$variable) vcf$Year <- str_extract(vcf$variable,"[[:digit:]]+") vcf$var <- sapply(strsplit(vcf$variable, "[[:digit:]]+"),"[[",2) vcf$variable <- NULL vcf <- dcast(vcf,rteno ~ var,fun.aggregate=mean) vcf$rteno <- as.character(vcf$rteno) vars <- merge(vcf,routeinfo,by="rteno") #set bird to the AOU code of your species of interest dataperspecies <- subset(BBS[which(BBS$Aou == bird),]) vars$abundance <- dataperspecies$count[match(vars$rteno, dataperspecies$rteno)] vars[is.na(vars)] <- 0 assess <- data.table(vars) whattodrop <- assess[,list(abundance=sum(abundance)),by='rteno'] droprtes <- whattodrop$rteno[whattodrop$abundance==0] vars <- vars[!(vars$rteno %in% droprtes),] vars <- vars[vars$RunType > 0,] vars <- vars[vars$RPID < 102]