# NOT RUN {
## Get data with length measurements and some assigned ages
data(WR79)
## First Example -- Even breaks for length categories
WR1 <- WR79
WR1$LCat <- lencat(WR1$len,w=5) # add length categories (width=5)
WR1.age <- subset(WR1, !is.na(age)) # isolate aged and unaged samples
WR1.len <- subset(WR1, is.na(age))
head(WR1.len) # note no ages in unaged sample
raw <- xtabs(~LCat+age,data=WR1.age) # create age-length key
( WR1.key <- prop.table(raw, margin=1) )
WR1.len <- alkIndivAge(WR1.key,age~len,data=WR1.len) # apply the age-length key
head(WR1.len) # now there are ages
WR1.comb <- rbind(WR1.age, WR1.len) # combine orig age & new ages
Summarize(len~age,data=WR1.comb,digits=2) # mean length-at-age
( af <- xtabs(~age,data=WR1.comb) ) # age frequency distribution
( ap <- prop.table(af) ) # proportional age distribution
## Second Example -- length sample does not have an age variable
WR2 <- WR79
WR2.age <- subset(WR2, !is.na(age)) # isolate age and unaged samples
WR2.len <- subset(WR2, is.na(age))
WR2.len <- WR2.len[,-3] # remove age variable (for demo only)
WR2.age$LCat <- lencat(WR2.age$len,w=5) # add length categories to aged sample
raw <- xtabs(~LCat+age,data=WR2.age) # create age-length key
( WR2.key <- prop.table(raw, margin=1) )
WR2.len <- alkIndivAge(WR2.key,~len,data=WR2.len) # apply the age-length key
WR2.len$LCat <- lencat(WR2.len$len,w=5) # add length cat to length sample
head(WR2.len) # now there are ages
WR2.comb <- rbind(WR2.age, WR2.len) # combine orig age & new ages
Summarize(len~age,data=WR2.comb,digits=2)
## Third Example -- Uneven breaks for length categories
WR3 <- WR79
brks <- c(seq(35,100,5),110,130) # set up uneven breaks
WR3$LCat <- lencat(WR3$len,breaks=brks) # add length categories (width=5)
WR3.age <- subset(WR3, !is.na(age)) # isolate aged and unaged samples
WR3.len <- subset(WR3, is.na(age))
head(WR3.len) # note no ages in length sample
raw <- xtabs(~LCat+age,data=WR3.age) # create age-length key
( WR3.key <- prop.table(raw, margin=1) )
WR3.len <- alkIndivAge(WR3.key,age~len,data=WR3.len,breaks=brks) # apply the age-length key
head(WR3.len) # now there are ages
WR3.comb <- rbind(WR3.age, WR3.len) # combine orig age & new ages
Summarize(len~age,data=WR3.comb,digits=2)
# }
Run the code above in your browser using DataLab