library(foreign) library(data.table) library(reshape2) library(stringr) #BIRD DATA BBS <- rbindlist(lapply(list.files("C:\\Users\\jmgorzo\\Dropbox\\model\\input\\bird/BBS/state_bbs_data",full.names=TRUE),read.csv)) BBS$countrynum <- NULL BBS$rteno <- as.integer(paste(BBS$statenum,formatC(BBS$Route, width=3, flag="0"),sep="")) BBS$SpeciesTotal <- NULL BBS$StopTotal <- NULL BBS <- melt(BBS, id.vars = c("Year","statenum","Route","Aou","rteno"),variable.name = "interval",value.name="count") BBS$route_piece <- paste(BBS$rteno,BBS$interval,sep="_") BBS$pieceID <- paste(BBS$route_piece,BBS$Year,sep="_") route_data <- read.csv("bird/BBS/routes.csv") route_data$rteno <- as.integer(paste(route_data$statenum,formatC(route_data$Route, width=3, flag="0"),sep="")) route_data <- route_data[c("BCR","rteno")] routeinfo <- read.csv("bird/BBS/weather.csv") routeinfo$rteno <- as.integer(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)]) #0L is indexing param, DO NOT FIDDLE WITH BBS <- merge(BBS,routeinfo,by=c("rteno","Year")) BBS <- merge(BBS,route_data,by="rteno") BBS <- BBS[BBS$RunType > 0,] BBS <- BBS[BBS$RPID < 102,] vcf_subroute <- read.dbf("landcover/bbs_segment_buffers.dbf") vcf_subroute$interval <- paste("count",vcf_subroute$segment,"0",sep="") vcf_subroute <- vcf_subroute[,c("rteno","interval",grep("[[:digit:]]", names(vcf_subroute), perl=TRUE, value=TRUE))] vcf_subroute <- melt(vcf_subroute,id=c("rteno","interval")) vcf_subroute$variable <- as.character(vcf_subroute$variable) vcf_subroute$Year <- str_extract(vcf_subroute$variable,"[[:digit:]]+") vcf_subroute$var <- sapply(strsplit(vcf_subroute$variable, "[[:digit:]]+"),"[[",2) vcf_subroute$variable <- NULL vcf_subroute <- dcast(vcf_subroute,rteno + interval ~ var,fun.aggregate=mean) vcf_subroute$rteno <- as.character(vcf_subroute$rteno)