gReshape | R Documentation |
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.
gReshape( df, in.pre, id.var, var.name = "prvAge", val.name = in.pre, last.plus = NULL, na.rm = TRUE, drop = NULL )
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 |
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 |
na.rm |
A logical that indicates whether |
drop |
A vector of variable names to drop before reshaping. |
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
.
Returns a data frame in one-measurement-per-line format. See details.
This code was in FSA but it can largely be accomplished with gather
from tidyr and str_sub
from stringr.
Derek H. Ogle, dogle@northland.edu
See also gConvert
for related code, and reshape
, and gather
in tidyr for similar but more general functionality.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.