#' @title Set classes for variables in WiDNR Fish Management Data.
#'
#' @description Set classes for variables in WiDNR Fish Management Data.
#'
#' @details This function is used to set the class type (e.g., integer, factor, etc.) for variables from the WiDNR Fish Management central database.
#'
#' @note THIS IS EXPERIMENTAL AT THIS POINT (and not sure that it is really needed ... read.csv might do an adequate job of guessing variable class type).
#'
#' @param df The data.frame that contains the Wi Fish Management Data (likely directly from the central database).
#' @param type A single string that indicates the variable name type in \code{df}. The three choices -- \code{"RDNR"}, \code{"DNR"}, and \code{"R"} -- are described in full in \code{\link{changeDBNames}}.
#' @param \dots Not yet implemented.
#'
#' @return A data.frame exactly as given in \code{data} except, possibly, with different classes for the variables.
#'
#' @author Derek H. Ogle, \email{dogle@@northland.edu}
#'
#' @keywords manip
#'
#' @examples
#' ftmp <- system.file("extdata", "FMDB_ex.csv",package="fishWiDNR")
#' ## Read in datafile (note periods in names) ... this is RDNR type of data
#' df1 <- read.csv(ftmp,stringsAsFactors=FALSE,na.strings="-")
#' str(df1)
#'
#' ## Set classes (using default 'RDNR' type)
#' df1a <- setFMDBClasses(df1)
#' tmp <- cbind(sapply(df1,class),sapply(df1a,class),
#' as.character(VarLookup$type[match(names(df1a),VarLookup$RDNR)]))
#' colnames(tmp) <- c("old","new","supposed to be")
#' tmp
#'
#' ## Set classes using R names (changed first using changeDBNames)
#' df1R <- changeDBNames(df1,from="RDNR","R")
#' df1Ra <- setFMDBClasses(df1R,type="R")
#' tmp <- cbind(sapply(df1R,class),sapply(df1Ra,class),
#' as.character(VarLookup$type[match(names(df1Ra),VarLookup$R)]))
#' colnames(tmp) <- c("old","new","supposed to be")
#' tmp
#'
#' ## test from partial list of RDNR names
#' df2 <- df1[,-c(1,4,7,10,16,32:47)]
#' df2a <- setFMDBClasses(df2)
#' tmp <- cbind(sapply(df2,class),sapply(df2a,class),
#' as.character(VarLookup$type[match(names(df2a),VarLookup[,"RDNR"])]))
#' colnames(tmp) <- c("old","new","supposed to be")
#' tmp
#'
#' @export setDBClasses
#' @rdname setFMDBClasses
setDBClasses <- function(df,type=c("RDNR","DNR","R"),...) {
warning("'setDBClasses()' is deprecated. Use 'setFMDBClasses()' instead.",call.=FALSE)
setFMDBClasses(df,type,...)
}
#' @export setFMDBClasses
#' @rdname setFMDBClasses
setFMDBClasses <- function(df,type=c("RDNR","DNR","R"),...) {
# match arguments and do some error checking
type <- match.arg(type)
# load VarLookup data frame into this function's environment
# the data/get combination are used to avoid the "no global binding" note at CHECK
VarLookup <- get(utils::data("VarLookup", envir = environment()), envir = environment())
# which rows lin VarLookup are found in the names of df2
wvar <- match(names(df),VarLookup[,type])
# cycle through the variables in df and change them to the corresponding type in VarLookup
for (i in 1:length(wvar)) {
tmp <- as.character(VarLookup$type[wvar[i]])
switch(tmp,
"factor" = df[,i] <- as.factor(df[,i]),
"numeric" = df[,i] <- as.numeric(gsub(",","",df[,i])),
"Date" = df[,i] <- lubridate::dmy(df[,i]),
"character" = df[,i] <- as.character(df[,i])
)
}
df
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.