if (FALSE) { # interactive()
# \donttest{
###EXAMPLE USING RAW DATA FROM Magalhaes et al., 2015###
data(Herichthys)#load data
#Subset out individuals with diet data
HMdat <- Herichthys[Herichthys$total==100,]
#Make a data frame of the individuals, lake by year, and lake.
HMtax <- cbind.data.frame(HMdat$individual,paste(HMdat$lake,HMdat$year),HMdat$lake)
#Name the data frame
colnames(HMtax) <- c("Individual","Lake x Year","Lake (all years)")
#To calculate trophic level for the entire species, add a vecotr to the data frame of
#the species name
HMtax$species <- "Herichthys minckleyi"
HMdat <- HMdat[,c("individual","X.Gastrop","X.Insect","X.Fish","X.Zoopl"
,"X.plants","X.algae", "X.detritus")]
#Repeat the individual name the number of unique prey types (6)
Inds <- rep(x = HMdat$individual, times=6)[order(rep(x = HMdat$individual, times=6))]
#Repeat the number of food typed the length of the number of individuals
FoodTypes <- rep(x = colnames(HMdat[2:7]),times=length(unique(HMtax$Individual)))
#Make a data frame, the length of the individuals with three columns
HM.mat <- as.data.frame(matrix(nrow = length(Inds),ncol = 3))
#Name these columns
colnames(HM.mat) <- c("Individual","FoodItem","Percent")
#Populate the dataframes first column with the individual and the second column with the prey type
HM.mat$Individual <- Inds
HM.mat$FoodItem <- FoodTypes
#Run this for loop to find the diet data based on the individual and then match the
#diet percentage based on the name of the prey type
for(i in 1:nrow(HMdat)){
rows2match<-which(HM.mat$Individual==HMdat$individual[i])
HM.mat$Percent[rows2match]<-as.vector(as.numeric(HMdat[i,2:7]))
}
#Remove prey that do not contribute to diets
HM.mat <- HM.mat[!HM.mat$Percent==0,]
#Create a empty data frame for prey values
PreyMat <- as.data.frame(matrix(ncol = 3,nrow = 6))
#Name the columns something useful
colnames(PreyMat)<-c("FoodItem","TL","SE")
#Add in the prey name to the PreyMat
PreyMat[,1] <- unique(FoodTypes)
#Add in the trophic levels of the prey
PreyMat[,2] <- c(2.37,2.2,3.5,2.1,2,2)
#Add in the SE of the prey
PreyMat[,3] <- c(.58,.4,.8,.3,0,0)
HM.TL <- DietTroph(DietItems = HM.mat,PreyValues = PreyMat, PreyClass = "FoodItem",
Taxonomy = HMtax, SumCheck = TRUE)
###EXAMPLE USING DATA FROM FISHBASE###
#Get some food item data from rfishbase
library(rfishbase)
#convert FishBase data into data for trophic calculation using dietr
converted.diet <- ConvertFishbaseDiet(ExcludeStage=NULL)
#Subset three studies out, as this contains all studies from FishBase
my.diets <- converted.diet$DietItems[1:26,]
my.taxonomy <- converted.diet$Taxonomy[1:3,]
#Load Prey Values
data(FishBasePreyVals)
#Calculate Trophic Levels
my.TL <- DietTroph(DietItems = my.diets,PreyValues = FishBasePreyVals, Taxonomy =
my.taxonomy, PreyClass=c("FoodI","FoodII","FoodIII","Stage"))
# }
}
Run the code above in your browser using DataLab