Are you saying that the dive data for a single whale with 8 dives is stored
in 8 separate files?
How are you reading these files into R?  read.table()?  Could you post an
example of the line of code that reads in the data?

Something like this might work for you.

# vector of file names
file.names <- c("dive1.csv", "dive2.csv", ... "diven.csv")

# read in all the files and save them into a list of data frames
dives <- lapply(file.names, read.csv)

# define a single function that does all of your computations
compute <- function(whale, depths, fish, zoop) {

## insert all the code in your e-mail here ##

# then list off all the variables you want to keep as output from the
function here
 list(fish.CG, whale.CB, zoop.CG, fish.I, whale.I, zoop.I, GIC.fish_whale,
GIC.zoop_whale)

}

# then you can run your function on every whale dive
# not sure how to fill in the arguments to the compute() function,
# because I don't know where whale, depths, fish, and zoop come from in
your code
results <- lapply(dives, function(dat) compute( ... ))

Jean


On Fri, Jun 21, 2013 at 5:18 PM, Bree W <bree.witteveen@alaska.edu> wrote:

> 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.
>

	[[alternative HTML version deleted]]

