-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b652755
commit ec7304c
Showing
9 changed files
with
213 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: SOFIA | ||
Version: 1.1.0 | ||
Date: 2022-02-20 | ||
Version: 1.2.0 | ||
Date: 2022-02-25 | ||
Title: Tools to Work with SOFIA Analyses | ||
Authors@R: c(person("Rishi", "Sharma", role="aut"), | ||
person("Arni", "Magnusson", role=c("aut","cre"), email="[email protected]")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#' 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 is a directory containing the primary data files. | ||
#' @param min.catch is a small numeric constant to use instead of \code{NA} or | ||
#' zero catches. | ||
#' @param quiet is 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 | ||
#' | ||
#' @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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.