Give this code a try. I tested it out on the data you posted. I used the depths from the prey data frame. Not sure if that's what you wanted to do. And, this time I guessed that you used the whale$Dive for your dive.freq. If not, you'll have to change that. (Note, this question was cross-posted to the r-help list, too.)
prey <- structure(list(depths = c(5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L,
45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L,
105L, 110L, 115L, 120L, 125L, 130L, 135L, 140L, 145L, 150L, 155L,
160L, 165L, 170L, 175L, 180L, 185L, 190L, 195L, 200L, 205L, 210L
), fish = c(0, 0, 0, 21.24194, 149.51694, 170.43214, 296.93453,
16.61643, 92.6813, 50.68548, 37.47343, 32.74443, 20.62983, 13.75121,
16.15562, 22.65562, 42.99768, 16315.65099, 43006.20482, 23476.2474,
30513.66346, 17411.645, 12127.70195, 4526.63393, 3328.89644,
1864.21486, 2202.07464, 2719.29417, 1710.75599, 2033.69552, 2796.81788,
967.09406, 437.30896, 193.15526, 143.88043, 406.31373, 786.30087,
1643.52542, 1665.69794, 1281.1579, 753.75309, 252.48867), zoop = c(0,
0, 0, 0, 14.937945, 0, 0.737109, 4.295556, 26.384844, 55.902301,
218.673781, 204.452678, 113.112452, 83.014457, 55.051358, 96.746271,
302.229135, 783.868978, 1713.133161, 3440.034642, 6667.914707,
9398.790964, 7580.233165, 7205.768739, 6567.175766, 4567.446886,
4295.772442, 4419.903403, 5102.68994, 4496.121974, 3269.193606,
2310.203528, 447.94014, 63.731336, 38.004799, 22.565211, 31.889927,
36.580063, 14.084152, 0, 35.343794, 0)), .Names = c("depths",
"fish", "zoop"), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36",
"37", "38", "39", "40", "41", "42"))
whale <- structure(list(Number = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L), Dive = c(95.1, 95.9, 95.1, 95.9, 96.8, 97.2, 96.8,
95.5, 94.2, 94.7, 94.2, 94.2, 95.9, 95.9, 93.8, 97.7, 99.4, 94.7,
92.5, 98.1, 97.2, 98.5, 95.5, 97.2, 98.5, 96.4, 94.7, 95.5),
Class = c("F", "F", "F", "F", "F", "F", "F", "N", "N", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "N", "N",
"N", "N", "N", "N", "N", "N")), .Names = c("Number", "Dive",
"Class"), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28"))
# split the data frame into a list with a different element for each dive
dives <- split(whale, whale$Dive)
# define a single function that does all of your computations
compute <- function(whale, depths, fish, zoop) {
# you don't say what part of the whale data you are counting ... I'll assume it's the dive
dive.freq <- table(cut(whale$Dive, c(0, depths)))
#compute Center of Gravity
fish.CG <- sum(depths*fish)/sum(fish) #calculate CG for fish distribution ONCE for each whale
zoop.CG <- sum(depths*zoop)/sum(zoop) #calculate CG for zoop distribution ONCE for each whale
whale.CG <- sum(depths*dive.freq/sum(dive.freq)) #calculate for EACH dive
#compute Inertia
fish.I <- sum((depths-fish.CG)^2*fish)/sum(fish)
zoop.I <- sum((depths-zoop.CG)^2*zoop)/sum(zoop)
whale.I <- sum((depths-whale.CG)^2*dive.freq)/sum(dive.freq) #needs to be calculated for EACH dive
# 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))
# then list off all the variables you want to keep as output from the function here
c(fish.CG=fish.CG, whale.CG=whale.CG, zoop.CG=zoop.CG, fish.I=fish.I, whale.I=whale.I, zoop.I=zoop.I,
GIC.fish_whale=GIC.fish_whale, GIC.zoop_whale=GIC.zoop_whale)
}
# apply the compute function to each element of the dives list
t(sapply(dives, function(dat) compute(whale=dat, depths=prey$depths, fish=prey$fish, zoop=prey$zoop)))