I have a complicated, multi-part question. My apologies if I do not make myself clear. I am also a fairly novice R user, so forgive me if this seems rudimentary. I want to calculate a index of colocation for whale dive data and prey distribution data. This entails:
Calculating a frequency distribution of whale depth of dive data BY DIVE into depth bins from prey (fish and zoop) data. For each dive, calculate the center of gravity (CG) and inertia (I). For each dive, calculate a global index of colocation (GIC) vs. each prey type. I want to be able to write a function (or series of functions) such that I do not have to separate my data by dive and rerun the functions for each dive manually. Example whale data, where number if the dive number (sometimes 40+ dives), dive is equal to the depth and classification is related to the type of dive it is. [IMG]http://i41.tinypic.com/33vc5rs.jpg[/IMG] Depth bins come from a separate data set containing prey information: [IMG]http://i43.tinypic.com/rjjy4n.jpg[/IMG] I have the following codes that work for the dive data as a whole, but need to write a loop or include an apply function such that I can run this for the data for each dive which is contained in a single file. So, for a whale with 40 dives, I need 40 whale frequencies, 40 whale CGs, 40 whale Is, etc. The prey distributionss are the SAME for each dive! Ultimately, I'd like a table which contains a list of the delta GIC values. #bin whale dive depths whale.cut=cut(whale,c(0 ,depths), right=FALSE) whale.freq=table(whale.cut) # compute CG fish.CG=sum(depths*fish)/sum(fish) whale.CG=sum(depths*whale.freq)/sum(whale.freq) zoop.CG=sum(depths*zoop)/sum(zoop) # compute Inertia fish.I=sum((depths-fish.CG)^2*fish)/sum(fish) whale.I=sum((depths-whale.CG)^2*whale.freq)/sum(whale.freq) zoop.I=sum((depths-zoop.CG)^2*zoop)/sum(zoop) #compute GIC as per # compute delta CG deltaCG.fish_whale=fish.CG-whale.CG GIC.fish_whale= 1-((deltaCG.fish_whale)^2/((deltaCG.fish_whale)^2+fish.I+whale.I)) deltaCG.zoop_whale=zoop.CG-whale.CG GIC.zoop_whale= 1-((deltaCG.zoop_whale)^2/((deltaCG.zoop_whale)^2+zoop.I+whale.I)) -- View this message in context: http://r.789695.n4.nabble.com/Calculating-an-index-of-colocation-for-a-large-dataset-tp4670084.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.