2013-06-21 73 views
0

我有一个复杂的多部分问题。如果我没有说清楚我的道歉。我也是一个相当新手的R用户,所以如果这看起来很简单,请原谅我。 我想计算鲸鱼潜水数据和猎物分布数据的托管索引。这需要:计算大型数据集R的代位索引

  1. 计算潜水数据鲸深度的BY DIVE从猎物(鱼和ZOOP)数据的频率分布到深度箱。
  2. 对于每次潜水,计算重心(CG)和惯性(I)。
  3. 对于每个潜水,计算一个全局索引的托管(GIC)与每个猎物类型 类型。

我希望能够写一个函数(或一系列的功能),使得我没有通过潜水手动分离我的数据,然后重新运行该功能,每个潜水。

示例鲸数据,其中如果潜水编号(有时为40+潜水),潜水数等于深度并且分类与潜水类型相关,则数字。 [IMG] http://i41.tinypic.com/33vc5rs.jpg[/IMG]

深度仓来自含有猎物信息的单独的数据集:

enter image description here

我有用于潜水数据作为一个整体的工作,但需要编写以下代码循环或包含应用函数,以便我可以为包含在单个文件中的每个潜水的数据运行此功能。因此,对于40次潜水的鲸鱼,我需要40个鲸鱼频率,40个鲸鱼CG,40个鲸鱼等等。猎物分布与每次潜水相同!最终,我想要一张包含增量GIC值列表的表格。

#bin whale dive depths 
dive.cut=cut(whale,c(0 ,depths), right=FALSE) 
dive.freq=table(dive.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)) 

UPDATES 我已粘贴的示例性数据两者猎物和鲸潜水。

猎物数据

depths  fish  zoop 
1  5  0.00000 0.000000 
2  10  0.00000 0.000000 
3  15  0.00000 0.000000 
4  20 21.24194 0.000000 
5  25 149.51694 14.937945 
6  30 170.43214 0.000000 
7  35 296.93453 0.737109 
8  40 16.61643 4.295556 
9  45 92.68130 26.384844 
10 50 50.68548 55.902301 
11 55 37.47343 218.673781 
12 60 32.74443 204.452678 
13 65 20.62983 113.112452 
14 70 13.75121 83.014457 
15 75 16.15562 55.051358 
16 80 22.65562 96.746271 
17 85 42.99768 302.229135 
18 90 16315.65099 783.868978 
19 95 43006.20482 1713.133161 
20 100 23476.24740 3440.034642 
21 105 30513.66346 6667.914707 
22 110 17411.64500 9398.790964 
23 115 12127.70195 7580.233165 
24 120 4526.63393 7205.768739 
25 125 3328.89644 6567.175766 
26 130 1864.21486 4567.446886 
27 135 2202.07464 4295.772442 
28 140 2719.29417 4419.903403 
29 145 1710.75599 5102.689940 
30 150 2033.69552 4496.121974 
31 155 2796.81788 3269.193606 
32 160 967.09406 2310.203528 
33 165 437.30896 447.940140 
34 170 193.15526 63.731336 
35 175 143.88043 38.004799 
36 180 406.31373 22.565211 
37 185 786.30087 31.889927 
38 190 1643.52542 36.580063 
39 195 1665.69794 14.084152 
40 200 1281.15790 0.000000 
41 205 753.75309 35.343794 
42 210 252.48867 0.000000 

鲸数据:

Number Dive Class 
1  1 95.1  F 
2  1 95.9  F 
3  1 95.1  F 
4  1 95.9  F 
5  1 96.8  F 
6  1 97.2  F 
7  1 96.8  F 
8  2 95.5  N 
9  2 94.2  N 
10  3 94.7  F 
11  3 94.2  F 
12  3 94.2  F 
13  3 95.9  F 
14  3 95.9  F 
15  4 93.8  F 
16  4 97.7  F 
17  4 99.4  F 
18  4 94.7  F 
19  4 92.5  F 
20  4 98.1  F 
21  5 97.2  N 
22  5 98.5  N 
23  5 95.5  N 
24  5 97.2  N 
25  5 98.5  N 
26  5 96.4  N 
27  5 94.7  N 
28  5 95.5  N 
+0

我看了一下链接,但无法找到什么'whale'指的是在你的代码。你可以给更多[完整(但最小)的数据集](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)?也尝试把它放在问题而不是链接。 – dardisco

+0

对不起。我错误地粘贴了旧版本的代码。在潜水深度的分档中,“鲸鱼”应该等于“潜水”。目前我不在我的办公室计算机,因此无法访问我的数据。如果上述说明不起作用,我会稍微模拟一些数据并重新发布。谢谢! – akbreezo

回答

1

给这个代码一试。我对你发布的数据进行了测试。我使用猎物数据框的深度。不知道这是你想做什么。而且,这一次我猜你已经使用了鲸鱼$潜水作为dive.freq。如果没有,你必须改变这一点。 (注意,这个问题是跨张贴到R-帮助名单,太。

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))) 
+0

我做了一些修改以适合我的数据,但这似乎是在正确的轨道上。什么是功能(dat)?当我运行结果代码时,我得到“错误:在计算(数据,深度,鱼,潜水)):尝试应用非函数。” – akbreezo

+0

在对数据进行测试后,我编辑了我的答案。做了一些改变。似乎现在正在工作。在'sapply()'函数中,'dat'是我赋予'dives'列表的每个元素的昵称。你可能想熟悉'lapply()'和'sapply()'函数。 –

+0

工作!谢谢! – akbreezo