lencat {FSA}R Documentation

Constructs length class/category variable.

Description

Constructs a new variable that contains the length class or category that an individual fish belongs to and attaches that new variable to the original data.frame.

Usage

lencat(df,cl,startcat=0,w=1,breaks=NULL,right=FALSE,vname="LCat",
       as.fact=TRUE,drop.levels=FALSE)

Arguments

df A data.frame that (minimally) contains a column of length measurements.
cl A number indicating which column the length measurements occupy in the data.frame, df.
startcat A number indicating the beginning of the first length-class.
w A number indicating the width of length classes to create.
breaks A numeric vector of lower values for the break points of the length categories.
right A logical indicating if the intervals should be closed on the right (and open on the left) or vice versa.
vname An optional string containing the name for the new length class variable.
as.fact A logical indicating if the new variable should be returned as a factor (=TRUE; default) or not.
drop.levels A logical indicating if the new variable should retain all levels indicated in breaks (=FALSE; default) or not. This is ignored if as.fact=FALSE.

Details

If breaks=NULL (the default) then this function will create length categories that begin with the value in startcat and continue by values of w until a category value greater than the largest observation in df[,cl]. In this instance categories of different widths are not allowed. The length categorizations are left-inclusive and right-exclusive by default (i.e., right=FALSE). The number in the startcat argument should be less than the smallest value in df[,cl]. In addition the number of decimals in startcat should not be more than the number of decimals in w. For example, startcat=0.4 and w=1 will result in the function being terminated with an error.

If breaks is non-NULL then startcat and w will be ignored. In this instance the vector of values in breaks should begin with a value at least as small as the minimum observation and end with a value at least as big as the maximum observation. If the lowest break value is larger than the minimum observation then the function will stop with an error. If the largest break is smaller than the maximum observation then an additional break larger than the maximum observation will be added to breaks. The use of breaks allows the use of length categories of different widths.

The observed values in the df[,cl] should be rounded to the appropriate number of decimals to avoid misplacement of individuals into incorrect length categories due to machine-precision issues. For example, see discussion in all.equal function.

If no variable name is supplied in vname then the default variable name will be LCat.

Value

Returns a data frame that consists of the original data frame, d, with the new length category variable appended and named as vname.

Author(s)

Derek H. Ogle, dogle@northland.edu

Examples

# random lengths measured to nearest 0.1 unit
len1 <- data.frame(len=round(runif(50,0.1,9.9),1))

# length categories by 0.1 unit starting at 0
len1a <- lencat(len1,"len",startcat=0,w=0.1)
table(len1a$LCat)

# length categories by 0.2 units starting at 0
len1b <- lencat(len1,"len",startcat=0,w=0.2)
table(len1b$LCat)

# length categories by 0.2 units starting at 0.1
len1c <- lencat(len1,"len",startcat=0.1,w=0.2)
table(len1c$LCat)

# length categories as set by breaks
len1d <- lencat(len1,"len",breaks=c(0,2,4,7,10))
table(len1d$LCat)

## A Second example
# random lengths measured to nearest unit
len2 <- data.frame(len=round(runif(50,10,117),0))    

# length categories by 5 units starting at 0
len2a <- lencat(len2,"len",startcat=0,w=5)
table(len2a$LCat)

# length categories by 5 units starting at 7
len2b <- lencat(len2,"len",startcat=7,w=5)
table(len2b$LCat)

# length categories by 10 units starting at 5
len2c <- lencat(len2,"len",startcat=5,w=10)
table(len2c$LCat)

# length categories as set by breaks
len2d <- lencat(len2,"len",breaks=c(5,50,75,150))
table(len2d$LCat)

## A Third example
# random lengths measured to nearest 0.1 unit
len3 <- data.frame(len=round(runif(50,10,117),1))

# length categories by 5 units starting at 0
len3a <- lencat(len3,"len",startcat=0,w=5)
table(len3a$LCat)

## A Fourth example
# random lengths measured to nearest 0.01 unit
len4 <- data.frame(len=round(runif(50,0.1,9.9),2))

# length categories by 0.1 unit starting at 0
len4a <- lencat(len4,"len",startcat=0,w=0.1)
table(len4a$LCat)

# length categories by 2 unit starting at 0
len4b <- lencat(len4,"len",startcat=0,w=2)
table(len4b$LCat)

# A Fifth example -- with real data
data(Croaker2)

# summarize to help find a good starting category
summary(Croaker2$tl)

# 10 mm length classes
Croaker2a <- lencat(Croaker2,"tl",startcat=200,w=10)
head(Croaker2a)

# 25 mm length classes
Croaker2a <- lencat(Croaker2a,"tl",startcat=200,w=25,vname="LenCat25")
head(Croaker2a)

[Package FSA version 0.0-13 Index]