R/create_enchist.R

Defines functions create_enchist

Documented in create_enchist

#' Generate encounter histories
#'
#' @param x A dataframe with columns for ResightDate/ResightPeriod and FlagID (usually generated by
#'          \code{format_bandedbirds()} and \code{set_period} if applicable).
#' @return Dataframes with encounter histories for each resighted bird that can be exported as CSV or INP files.
#' @examples
#' create_enchist(resight_data)

create_enchist <- function(x) {

#data=data.frame(data);

# now, we reshape the data using melt
transform=melt(x, id.vars="FlagID")
enchist=cast(transform, FlagID ~ value)

# remove row with encounter history for NA (no flags resighted that survey period)
enchist <- enchist[complete.cases(enchist), ]

enchist[is.na(enchist)]=0

# turns all non-zero matrix elements (for ResightPeriod, not FlagID) into 1
# following assumes Flag is in the first column of enchist
enchist[,2:ncol(enchist)][enchist[,2:ncol(enchist)] != 0] = 1

#send encounter history to global environment in list format
assign("enchist_csv", enchist, envir = globalenv())

# now do formatting of encounter history file for .inp file for MARK
enchist$eh <- apply(enchist[2:ncol(enchist)],1,paste,collapse="") # concatenates encounter columns into eh
enchist[2:(ncol(enchist)-1)] <- NULL # drops individual encounter columns

# create commented tag
enchist$FlagID <- paste("/*", enchist$FlagID, "*/", sep=" ")

# sort by descending encounter histories
enchist <- enchist[order(enchist$eh,decreasing=TRUE),]

# tack on the frequency for the individual
enchist$end <- "1;";
assign("enchist_inp", enchist, envir = globalenv())

}
ajmacdonald3/bb2enchist documentation built on Nov. 5, 2019, 4:47 p.m.