#' Convert Data
#'
#' Convert primary single-stock data files into combined data files ready for
#' analysis, such \code{catches.csv}, \code{effort.csv}, and/or
#' \code{indices.csv}.
#'
#' @param subdir directory containing the primary data files.
#' @param min.catch small numeric constant to use instead of \code{NA} or zero
#' catches.
#' @param quiet whether to suppress screen output.
#'
#' @details
#' \code{subdir} is usually one of \code{"both"}, \code{"effort"},
#' \code{"index"}, or \code{"neither"}, generated by the \code{groupData}
#' function.
#'
#' Pass any non-numeric value for \code{min.catch}, for example \code{FALSE}, to
#' retain \code{NA} and zero catch values.
#'
#' @return
#' Files are converted inside \code{subdir}. As a byproduct, a list is returned,
#' describing the files after conversion.
#'
#' @note
#' A primary data file can have a filename such as
#' \file{Yellowtail_snapper_Mexico.csv} and columns such as
#' \code{stockid|scientificname|commonname|year|catch|stocklong}.
#'
#' The combined data files, such as \code{catches.csv}, have the format
#' \code{Year|Unique_Stock_Name_1|Unique_Stock_Name_2|...} where, for example,
#' \code{Yellowtail_snapper_Mexico} is one column name.
#'
#' The output files created will depend on whether the data include effort
#' and/or index data. If \code{subdir = "both"}, a full set of output files will
#' be created: \code{catches.csv}, \code{effort.csv}, and \code{index.csv}.
#'
#' The functions \code{groupData} and \code{convertData} are used together:
#' first group, then convert.
#'
#' @author Arni Magnusson.
#'
#' @seealso
#' \code{\link{groupData}} groups primary data in subdirectories.
#'
#' \code{\link{SOFIA-package}} gives an overview of the package.
#'
#' @examples
#' \dontrun{
#' convertData("Data_files_Area_31_3/both")
#' convertData("Data_files_Area_31_3/effort, quiet=TRUE")
#' }
#'
#' @importFrom utils read.csv
#' @importFrom TAF write.taf
#'
#' @export
convertData <- function(subdir, min.catch=0.001, quiet=FALSE)
{
## 1 Import CSV files
files <- dir(subdir)
files <- files[!(files %in% c("catch.csv", "effort.csv", "index.csv"))]
if(length(files) == 0)
{
if(!quiet)
message("No CSV files to convert found in '", subdir, "'")
return(invisible(NULL))
}
csv <- lapply(file.path(subdir, files), read.csv)
names(csv) <- files
## 2 Prepare data frames
years <- range(unlist(lapply(csv, "[[", "year")))
years <- seq(min(years), max(years))
catch <- effort <- index <- data.frame(year=years)
## 3 Fill data frames
for(i in seq_along(files))
{
this <- csv[[i]]
names(this) <- tolower(names(this))
stock <- gsub("^Area[0-9]*_*", "", this$stockid[1]) # shave off prefix
if(!("year" %in% names(this)))
stop(files[i], " has no 'year' column")
if(!("catch" %in% names(this)))
stop(files[i], " has no 'catch' column")
c.vector <- merge(catch, this, all=TRUE)$catch
e.vector <- merge(effort, this, all=TRUE)$best_effort
i.vector <- merge(index, this, all=TRUE)$best_index
if(is.numeric(min.catch) && min.catch > 0)
c.vector[is.na(c.vector) | c.vector == 0] <- min.catch
catch[stock] <- c.vector
effort[stock] <- e.vector
index[stock] <- i.vector
}
## 4 Write data frames
group <- basename(subdir)
write.taf(catch, dir=subdir)
if(group == "effort" || group == "both")
write.taf(effort, dir=subdir)
if(group == "index" || group == "both")
write.taf(index, dir=subdir)
if(!quiet)
message("Converted ", length(files), " files")
## 5 Remove old files
file.remove(file.path(subdir, files))
## 6 Return list
out <- list(catch=catch)
if(group == "effort" || group == "both")
out$effort <- effort
if(group == "index" || group == "both")
out$index <- index
invisible(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.