diff --git a/NAMESPACE b/NAMESPACE index 1c8db97..3c61c3b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(clean.library) export(clean.software) export(convert.spaces) export(cp) +export(ddim) export(deps) export(detach.packages) export(div) @@ -23,7 +24,6 @@ export(draft.software) export(ds.file) export(ds.package) export(extract.subdir) -export(fdim) export(file.encoding) export(flr2taf) export(get.remote.sha) diff --git a/NEWS.md b/NEWS.md index e59a7ec..1073818 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ -# TAF 4.3.0 (2023-11-26) +# TAF 4.3.0 (2023-12-11) -* Added function fdim() to show the dimensions of an FLR table. +* Added function ddim() to show the data dimensions of a table. * Added function taf.libraries() to load all packages from TAF library. Code contributed by Iago Mosqueira. diff --git a/R/TAF-package.R b/R/TAF-package.R index c5ac98f..8209e72 100644 --- a/R/TAF-package.R +++ b/R/TAF-package.R @@ -61,8 +61,8 @@ #' } #' \emph{Tables:} #' \tabular{ll}{ +#' \code{\link{ddim}} \tab show data dimensions\cr #' \code{\link{div}} \tab divide column values\cr -#' \code{\link{fdim}} \tab show FLR dimensions\cr #' \code{\link{flr2taf}} \tab convert FLR to TAF\cr #' \code{\link{long2taf}} \tab convert long format to TAF\cr #' \code{\link{long2xtab}} \tab convert long format to crosstab\cr diff --git a/R/ddim.R b/R/ddim.R new file mode 100644 index 0000000..2c5de45 --- /dev/null +++ b/R/ddim.R @@ -0,0 +1,47 @@ +#' Data Dimensions +#' +#' Show the data dimensions of a table. +#' +#' @param x a data frame where the first columns are dimension variables and the +#' last column is a measurement variable. +#' @param reduce is whether to omit single-level dimensions. +#' +#' @details +#' Dimension variables can include year, age, region, fleet, survey, or the +#' like, generally an integer or string. The measurement variable can be catch, +#' fishing mortality, maturity, weight, or the like, often a decimal. +#' +#' \code{x} can also be an FLR table in \code{FLQuant} format. +#' +#' @return Named vector showing the dimension names and number of levels. +#' +#' @seealso +#' \code{\link{unique}} is the base function to extract the levels of a +#' dimension variable. +#' +#' \code{\link{TAF-package}} gives an overview of the package. +#' +#' @examples +#' # Long table format, 8 years and 4 ages +#' ddim(catage.long) +#' +#' # Some base R datasets +#' ddim(esoph[-5]) +#' ddim(rev(warpbreaks)) +#' ddim(rev(ToothGrowth)) +#' +#' @importFrom methods is +#' @importFrom stats setNames +#' +#' @export + +ddim <- function(x, reduce=FALSE) +{ + out <- if(is(x, "FLQuant")) + setNames(dim(x), names(dimnames(x))) + else + sapply(x, function(col) length(unique(col)))[-ncol(x)] + if(reduce) + out <- out[out != 1] + out +} diff --git a/R/fdim.R b/R/fdim.R deleted file mode 100644 index 59197e9..0000000 --- a/R/fdim.R +++ /dev/null @@ -1,56 +0,0 @@ -#' FLR Dimensions -#' -#' Show the dimensions of an FLR table. -#' -#' @param x an FLR table in \code{FLQuant} format or in a 7-column -#' \code{data.frame} format. -#' @param reduce is whether to omit single-level dimensions. -#' -#' @details -#' \code{x} can also be an arbitrary data frame (not related to FLR) where the -#' last column is the measurement variable. -#' -#' @return Named vector showing the dimension names and number of levels. -#' -#' @seealso -#' \code{\link{dim}}, \code{\link{dimnames}}, and \code{\link{names}} are the -#' base functions to access dimension names and levels. -#' -#' \code{\link{as.data.frame}} is a method provided by the \pkg{FLCore} package -#' to convert \code{FLQuant} tables to a 7-column long format. -#' -#' \code{\link{flr2taf}} converts an FLR table to TAF format. -#' -#' \code{\link{TAF-package}} gives an overview of the package. -#' -#' @examples -#' \dontrun{ -#' library(FLCore) -#' flq <- FLQuant(t(catage.xtab), dimnames=list(age=1:4, year=1963:1970)) -#' df <- as.data.frame(flq) -#' fdim(flq) -#' fdim(df) -#' fdim(df, TRUE) -#' } -#' -#' # Any data frame where the last column is the measurement variable -#' fdim(catage.long) -#' fdim(esoph[-5]) -#' fdim(rev(warpbreaks)) -#' fdim(rev(ToothGrowth)) -#' -#' @importFrom methods is -#' @importFrom stats setNames -#' -#' @export - -fdim <- function(x, reduce=FALSE) -{ - out <- if(is(x, "FLQuant")) - setNames(dim(x), names(dimnames(x))) - else - sapply(x, function(col) length(unique(col)))[-ncol(x)] - if(reduce) - out <- out[out != 1] - out -} diff --git a/R/flr2taf.R b/R/flr2taf.R index 80137c2..9b2c2db 100644 --- a/R/flr2taf.R +++ b/R/flr2taf.R @@ -17,8 +17,6 @@ #' \code{\link{as.data.frame}} is a method provided by the \pkg{FLCore} package #' to convert \code{FLQuant} tables to a 7-column long format. #' -#' \code{\link{fdim}} shows the dimensions of an FLR table. -#' #' \code{\link{TAF-package}} gives an overview of the package. #' #' @examples diff --git a/man/TAF-package.Rd b/man/TAF-package.Rd index 36e2014..9439e3b 100644 --- a/man/TAF-package.Rd +++ b/man/TAF-package.Rd @@ -60,8 +60,8 @@ scientific applications. } \emph{Tables:} \tabular{ll}{ + \code{\link{ddim}} \tab show data dimensions\cr \code{\link{div}} \tab divide column values\cr - \code{\link{fdim}} \tab show FLR dimensions\cr \code{\link{flr2taf}} \tab convert FLR to TAF\cr \code{\link{long2taf}} \tab convert long format to TAF\cr \code{\link{long2xtab}} \tab convert long format to crosstab\cr diff --git a/man/ddim.Rd b/man/ddim.Rd new file mode 100644 index 0000000..9f10be7 --- /dev/null +++ b/man/ddim.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ddim.R +\name{ddim} +\alias{ddim} +\title{Data Dimensions} +\usage{ +ddim(x, reduce = FALSE) +} +\arguments{ +\item{x}{a data frame where the first columns are dimension variables and the +last column is a measurement variable.} + +\item{reduce}{is whether to omit single-level dimensions.} +} +\value{ +Named vector showing the dimension names and number of levels. +} +\description{ +Show the data dimensions of a table. +} +\details{ +Dimension variables can include year, age, region, fleet, survey, or the +like, generally an integer or string. The measurement variable can be catch, +fishing mortality, maturity, weight, or the like, often a decimal. + +\code{x} can also be an FLR table in \code{FLQuant} format. +} +\examples{ +# Long table format, 8 years and 4 ages +ddim(catage.long) + +# Some base R datasets +ddim(esoph[-5]) +ddim(rev(warpbreaks)) +ddim(rev(ToothGrowth)) + +} +\seealso{ +\code{\link{unique}} is the base function to extract the levels of a +dimension variable. + +\code{\link{TAF-package}} gives an overview of the package. +} diff --git a/man/fdim.Rd b/man/fdim.Rd deleted file mode 100644 index 4025415..0000000 --- a/man/fdim.Rd +++ /dev/null @@ -1,52 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/fdim.R -\name{fdim} -\alias{fdim} -\title{FLR Dimensions} -\usage{ -fdim(x, reduce = FALSE) -} -\arguments{ -\item{x}{an FLR table in \code{FLQuant} format or in a 7-column -\code{data.frame} format.} - -\item{reduce}{is whether to omit single-level dimensions.} -} -\value{ -Named vector showing the dimension names and number of levels. -} -\description{ -Show the dimensions of an FLR table. -} -\details{ -\code{x} can also be an arbitrary data frame (not related to FLR) where the -last column is the measurement variable. -} -\examples{ -\dontrun{ -library(FLCore) -flq <- FLQuant(t(catage.xtab), dimnames=list(age=1:4, year=1963:1970)) -df <- as.data.frame(flq) -fdim(flq) -fdim(df) -fdim(df, TRUE) -} - -# Any data frame where the last column is the measurement variable -fdim(catage.long) -fdim(esoph[-5]) -fdim(rev(warpbreaks)) -fdim(rev(ToothGrowth)) - -} -\seealso{ -\code{\link{dim}}, \code{\link{dimnames}}, and \code{\link{names}} are the -base functions to access dimension names and levels. - -\code{\link{as.data.frame}} is a method provided by the \pkg{FLCore} package -to convert \code{FLQuant} tables to a 7-column long format. - -\code{\link{flr2taf}} converts an FLR table to TAF format. - -\code{\link{TAF-package}} gives an overview of the package. -} diff --git a/man/flr2taf.Rd b/man/flr2taf.Rd index ebd8c2e..9f80e7c 100644 --- a/man/flr2taf.Rd +++ b/man/flr2taf.Rd @@ -38,7 +38,5 @@ flr2taf(x1, "Juveniles") \code{\link{as.data.frame}} is a method provided by the \pkg{FLCore} package to convert \code{FLQuant} tables to a 7-column long format. -\code{\link{fdim}} shows the dimensions of an FLR table. - \code{\link{TAF-package}} gives an overview of the package. } diff --git a/tests/Examples/TAF-Ex.Rout.save b/tests/Examples/TAF-Ex.Rout.save index 375aaf1..077acbe 100644 --- a/tests/Examples/TAF-Ex.Rout.save +++ b/tests/Examples/TAF-Ex.Rout.save @@ -1,5 +1,5 @@ -R version 4.3.1 (2023-06-16) -- "Beagle Scouts" +R version 4.3.2 (2023-10-31) -- "Eye Holes" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) @@ -353,6 +353,37 @@ Type 'q()' to quit R. > > > cleanEx() +> nameEx("ddim") +> ### * ddim +> +> flush(stderr()); flush(stdout()) +> +> ### Name: ddim +> ### Title: Data Dimensions +> ### Aliases: ddim +> +> ### ** Examples +> +> # Long table format, 8 years and 4 ages +> ddim(catage.long) +Year Age + 8 4 +> +> # Some base R datasets +> ddim(esoph[-5]) +agegp alcgp tobgp + 6 4 4 +> ddim(rev(warpbreaks)) +tension wool + 3 2 +> ddim(rev(ToothGrowth)) +dose supp + 3 2 +> +> +> +> +> cleanEx() > nameEx("deps") > ### * deps > @@ -550,44 +581,6 @@ Type 'q()' to quit R. > > > cleanEx() -> nameEx("fdim") -> ### * fdim -> -> flush(stderr()); flush(stdout()) -> -> ### Name: fdim -> ### Title: FLR Dimensions -> ### Aliases: fdim -> -> ### ** Examples -> -> ## Not run: -> ##D library(FLCore) -> ##D flq <- FLQuant(t(catage.xtab), dimnames=list(age=1:4, year=1963:1970)) -> ##D df <- as.data.frame(flq) -> ##D fdim(flq) -> ##D fdim(df) -> ##D fdim(df, TRUE) -> ## End(Not run) -> -> # Any data frame where the last column is the measurement variable -> fdim(catage.long) -Year Age - 8 4 -> fdim(esoph[-5]) -agegp alcgp tobgp - 6 4 4 -> fdim(rev(warpbreaks)) -tension wool - 3 2 -> fdim(rev(ToothGrowth)) -dose supp - 3 2 -> -> -> -> -> cleanEx() > nameEx("file.encoding") > ### * file.encoding > @@ -1825,7 +1818,7 @@ detaching ‘package:lattice’ > options(digits = 7L) > base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n") -Time elapsed: 0.225 0.008 0.234 0 0 +Time elapsed: 0.236 0.001 0.236 0 0 > grDevices::dev.off() null device 1