## Band Combos # Determine unused color band combos, export this to Excel, and calculate the number of bands per color needed and cost. #Updated 6 July 2011 #Install the gregmisc package library("gregmisc") # Create list of all possible band combinations: bands<-c("AL", "DB", "DG", "K", "LB", "LG", "O", "PI", "R", "V", "W", "Y") possible.combos<-permutations(12, 4, bands, repeats.allowed=TRUE) #12 refers to number of possible colors. 4 is number of bands a bird can carry. data.class(possible.combos) combos.with.AL <- possible.combos[rowSums(possible.combos=="AL") %in% c(1),] #Extract only combos that include one aluminum band. head(combos.with.AL) combos.with.AL NROW(combos.with.AL) # Create list of unique band combos used: b=read.csv("yourfilepath/yourfilename.csv", header=TRUE) #Replace your banding database filename here. b2=b[b$Species=="SWSP", c("Upper.right", "Lower.right", "Upper.left", "Lower.left")] #Select the species you want and the columns containing bands. b2 NROW(b2) b3=unique(b2) b4=as.matrix(b3) # Remove used combos from possible combo list. Logic supplied by David Winsemius at Heritage Laboratories. unused.combos=combos.with.AL[apply(combos.with.AL, 1, function(x) max(apply(b4, 1, function(y) all.equal(x, y, check.attributes=FALSE)) ) ) !="TRUE" , ] # THIS TAKES A WHILE (~3 minutes)! max(c(TRUE,FALSE))==TRUE; cycle through all of 2nd mtx unused.combos NROW(unused.combos) # Remove combos that include certain colors. Ex. DG and DB can be confused. LB often looks like W. PI is too bright. combos.without.DG <- unused.combos[rowSums(unused.combos=="DG")==0,] combos.without.DG.DB <- combos.without.DG[rowSums(combos.without.DG=="DB")==0,] NROW(combos.without.DG.DB) combos.without.DG.DB.LB <- combos.without.DG.DB[rowSums(combos.without.DG.DB=="LB")==0,] NROW(combos.without.DG.DB.LB) combos.without.DG.DB.LB.PI <- combos.without.DG.DB.LB[rowSums(combos.without.DG.DB.LB=="PI")==0,] NROW(combos.without.DG.DB.LB.PI) combos.without.DG.DB.LB.PI # Subset number of combos that you need. Here I take the first 250 combos. new.combos<-combos.without.DG.DB.LB.PI[1:250,] new.combos # Export combos to .csv colnames(new.combos)=c("Upper.right", "Lower.right", "Upper.left", "Lower.left") write.csv(new.combos, file = "new.combos.csv", row.names = FALSE) # to read .csv file back into R: read.csv("new.combos.csv") # Count number of each band needed. t=table(new.combos[,1:4]) t # Calculate cost of bands (as of Dec. 2009, color bands are $0.25 ea.). (sum(t)-250)*0.25 # Subtract the 250 AL bands.