#===== Create random data for three species
#----- just to control the randomization
set.seed(345234534)
dbt <- data.frame(species=factor(rep(c("Bluefin Tuna"),30)),
tl=round(rnorm(30,1900,300),0))
dbt$wt <- round(4.5e-05*dbt$tl^2.8+rnorm(30,0,6000),1)
dbg <- data.frame(species=factor(rep(c("Bluegill"),30)),
tl=round(rnorm(30,130,50),0))
dbg$wt <- round(4.23e-06*dbg$tl^3.316+rnorm(30,0,10),1)
dlb <- data.frame(species=factor(rep(c("Largemouth Bass"),30)),
tl=round(rnorm(30,350,60),0))
dlb$wt <- round(2.96e-06*dlb$tl^3.273+rnorm(30,0,60),1)
df <- rbind(dbt,dbg,dlb)
str(df)
#===== Add Wr variable
#----- using formula interface
df$Wr1 <- wrAdd(wt~tl+species,data=df)
#----- same but with non-formula interface
df$Wr2 <- wrAdd(df$wt,df$tl,df$species)
#----- same but using dplyr
if (require(dplyr)) {
df <- df %>%
mutate(Wr3=wrAdd(wt,tl,species))
}
#----- examine results
peek(df,n=10)
#===== Example with only one species in the data.frame
bg <- droplevels(subset(df,species=="Bluegill"))
bg$Wr4 <- wrAdd(wt~tl+species,data=bg)
bg
#===== Example with a species that has Ws eqns for multiple groups and a
# group needs to be specified with WsOpts
wae <- data.frame(species=factor(rep(c("Walleye"),30)),
tl=round(rnorm(30,500,200),0))
wae$wt <- round(3.33e-06*wae$tl^3.16+rnorm(30,0,50),1)
# wae$Wr <- wrAdd(wt~tl+species,data=wae) # will err b/c multiple groups
wae$Wr <- wrAdd(wt~tl+species,data=wae,
WsOpts=list(Walleye=list(group="overall")))
peek(wae,n=10)
Run the code above in your browser using DataLab