Skip to content

Commit

Permalink
New function convertData()
Browse files Browse the repository at this point in the history
  • Loading branch information
arni-magnusson committed Feb 25, 2022
1 parent b652755 commit ec7304c
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 9 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
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]"))
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(addDriors)
export(addEffort)
export(calcCat)
export(compCat)
export(convertData)
export(groupData)
export(plotCat)
export(plotProp)
Expand Down
13 changes: 12 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# SOFIA 1.2.0 (2022-02-25)

* Added function convertData() to convert primary data to combined data.

* Improved groupData() so it exits gracefully when no CSV files are found.
Furthermore, it always creates a new set of empty subdirectories for the
output before copying files.




# SOFIA 1.1.0 (2022-02-20)

* Added function groupData() to group primary data into subdirectories.
* Added function groupData() to group primary data in subdirectories.



Expand Down
7 changes: 4 additions & 3 deletions R/SOFIA-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#' @details
#' \emph{Prepare data:}
#' \tabular{ll}{
#' \code{\link{addDriors}} \tab add driors column to stocks object\cr
#' \code{\link{addEffort}} \tab add effort column to catch data\cr
#' \code{\link{groupData}} \tab group primary data in subdirectories
#' \code{\link{addDriors}} \tab add driors column to stocks object\cr
#' \code{\link{addEffort}} \tab add effort column to catch data\cr
#' \code{\link{convertData}} \tab convert primary data to combined data\cr
#' \code{\link{groupData}} \tab group primary data in subdirectories
#' }
#' \emph{Calculate:}
#' \tabular{ll}{
Expand Down
116 changes: 116 additions & 0 deletions R/convertData.R
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)
}
5 changes: 5 additions & 0 deletions R/groupData.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@
#' both + (effort-both) + (index-both) + neither
#' }
#'
#' The functions \code{groupData} and \code{convertData} are used together:
#' first group, then convert.
#'
#' @author Arni Magnusson.
#'
#' @seealso
#' \code{\link{convertData}} converts primary data to combined data.
#'
#' \code{\link{SOFIA-package}} gives an overview of the package.
#'
#' @examples
Expand Down
7 changes: 4 additions & 3 deletions man/SOFIA-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions man/convertData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/groupData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec7304c

Please sign in to comment.