#===== Create random data for three species
set.seed(345234534)
dbg <- data.frame(species=factor(rep(c("Bluegill"),30)),
tl=round(rnorm(30,130,50),0))
dlb <- data.frame(species=factor(rep(c("Largemouth Bass"),30)),
tl=round(rnorm(30,350,60),0))
dbt <- data.frame(species=factor(rep(c("Bluefin Tuna"),30)),
tl=round(rnorm(30,1900,300),0))
df <- rbind(dbg,dlb,dbt)
#===== Simple examples
#----- Add variable using category names -- non-formula notation
df$PSD <- psdAdd(df$tl,df$species)
peek(df,n=6)
#----- Add variable using category names -- formula notation
df$PSD1 <- psdAdd(tl~species,data=df)
peek(df,n=6)
#----- Add variable using length values as names
# Also turned off messaging of fish not in PSDlit
df$PSD2 <- psdAdd(tl~species,data=df,use.names=FALSE,verbose=FALSE)
peek(df,n=6)
#----- Same as above but using dplyr
if (require(dplyr)) {
df <- df %>%
mutate(PSD1A=psdAdd(tl,species,verbose=FALSE),
PSD2A=psdAdd(tl,species,use.names=FALSE,verbose=FALSE))
peek(df,n=6)
}
#===== Adding lengths besides the Gabelhouse lengths
#----- Add a "minimum length" for Bluegill
df$PSD3 <- psdAdd(tl~species,data=df,verbose=FALSE,
addLens=list("Bluegill"=c("minLen"=175)))
df$PSD3A <- psdAdd(tl~species,data=df,verbose=FALSE,
addLens=list("Bluegill"=175))
df$PSD3B <- psdAdd(tl~species,data=df,verbose=FALSE,
addLens=list("Bluegill"=c("minLen"=175)),use.names=FALSE)
head(df,n=6)
#----- Add add'l lengths and names for Bluegill and Largemouth Bass
df$psd4 <- psdAdd(tl~species,data=df,verbose=FALSE,
addLens=list("Bluegill"=175,
"Largemouth Bass"=c(254,356)))
peek(df,n=20)
#===== Example for a species with sub-groups
dbt <- data.frame(species=factor(rep(c("Brown Trout"),30)),
tl=round(rnorm(30,230,50),0))
dlt <- data.frame(species=factor(rep(c("Lake Trout"),30)),
tl=round(rnorm(30,550,60),0))
df2 <- rbind(dbt,dlt)
df2$psd <- psdAdd(tl~species,data=df2,group=list("Brown Trout"="lentic"))
peek(df2,n=6)
Run the code above in your browser using DataLab