gReshape: Reshapes a one-fish-per-line data frame to a...

View source: R/gReshape.R

gReshapeR Documentation

Reshapes a one-fish-per-line data frame to a one-measurement-per-line data frame.

Description

Converts growth data from the one-fish-per-line format to the one-measurement-per-line format. One-fish-per-line format is a common form for collecting or storing growth data. One-measurement-per-line format is required for many statistical analyses.

Usage

gReshape(
  df,
  in.pre,
  id.var,
  var.name = "prvAge",
  val.name = in.pre,
  last.plus = NULL,
  na.rm = TRUE,
  drop = NULL
)

Arguments

df

A data frame that contains the growth measurement data in one-fish-per-line format with specifics as defined in the details.

in.pre

A string that represents the common start to the measurement variable names. See details.

id.var

A vector of variables in df that do not change. See details.

var.name

A string that indicates the name in the reshaped data frame that represents the level of the measurement variables in the original data frame.

val.name

A string that indicates the name in the reshaped data frame that represents the measurements in the orginal data frame.

last.plus

A string that if non-null indicates that the last measurement represents “plus growth” and not an actual increment. If “plus growth” is recorded then this string should indicate the name of the variable in df that contains the age of the fish at capture.

na.rm

A logical that indicates whether NA values in the measurement variables should be removed after reshaping.

drop

A vector of variable names to drop before reshaping.

Details

This function does NOT convert the data from radial to incremental or incremental to radial measurements (see gConvert).

The input data frame in df must have the following specific formats. First, the measurements of annular increments or radii must be in one-fish-per-line format. The measurements must be contained in columns that are named with a common prefix (e.g., “anu”, “inc”, or “rad”) followed by a number that represents the age of the fish when that portion of the structure formed. This prefix must be the same for all columns that contains measurements and must not be found in any other variable (as a prefix or not). For example, the first annular measurement should be in a variable named “anu1”, the second annular measurement in “anu2”, and so on. The name of the prefix should be included in the in.pre= argument.

If id.var is left blank then the vector of variables that will not be changed upon the reshaping will consist of all variables that do NOT start with the in.pre prefix.

Errors may occur if a particular variable in the original data frame, to be included in the id.var= list, is of POSIX type. A workaround for this error is to include the name of that variable in drop=.

The name of the variable in the reshaped output data frame that contains the measurements will be called the same as in.pre by default. This can be changed by including a new name as a string in val.name.

Value

Returns a data frame in one-measurement-per-line format. See details.

Note

This code was in FSA but it can largely be accomplished with gather from tidyr and str_sub from stringr.

Author(s)

Derek H. Ogle, dogle@northland.edu

See Also

See also gConvert for related code, and reshape, and gather in tidyr for similar but more general functionality.

Examples

data(SMBassWB,package="FSA")
head(SMBassWB)

# convert radial measurements to increments
SMBi1 <- RFishBC::gConvert(SMBassWB,in.pre="anu",out.type="inc")
head(SMBi1)

SMBi1a <- gReshape(SMBi1,in.pre="inc")
head(SMBi1a)

# same as above but assume that last increment (in agecap variable) is plus-growth
SMBi2a <- gReshape(SMBi1,in.pre="inc",last.plus="agecap")
head(SMBi2a)

# example of dropping some variables before reshaping
SMBi1b <- gReshape(SMBi1,in.pre="inc",drop=c("species","lake"))
head(SMBi1b)


droglenc/FSAmisc documentation built on Jan. 8, 2023, 12:59 a.m.