整合深度整合物种丰度的函数

问题描述

嘿,

我正在尝试计算整个深度范围(例如,从 10 m 到 90 m)内每个类别的生物数量。为此,我在某些深度(例如 10、30 和 90 m)拥有丰度,并使用积分函数计算: 每对深度之间的丰度平均值,乘以深度对的差异。这些值在整个深度水柱上相加,以获得水柱上的总丰度。 看一个例子(只是大数据集的一小部分,有多个地点和年份,更多的类别和深度):

ProvisioningParameters

但是我得到了这些结果和带有 NA 值的行的警告消息:

View(df)
      Class      Depth   organismQuantity
1   Ciliates        10  1608.89
2   Ciliates        30  2125.09
3   Ciliates        90  1184.92
4   Dinophyceae     10  0.00
5   Dinoflagellates 30  28719.60
6   Dinoflagellates 90  4445.26



integrate = function(x) {
  averages = (x$organismQuantity[1:length(x)-1] + x$organismQuantity[2:length(x)]) / 2
  sum(averages * diff(x$Depth))
}
result = ddply(df,.(Class),integrate)
print(result)

我不明白为什么 Dinoflagellates 有 NA 值......我的完整数据集中的其他几个类也是如此(对于某些类丰度,积分方程适用于其他类,我收到了警告消息)。 感谢您的帮助!! 干杯, 露西

解决方法

这是一种使用 trapz 包中的函数 caTools 的方法,适用于该问题。

#
# library(caTools)
# Author(s)
# Jarek Tuszynski
#
# Original,adapted
trapz <- function(DF,x,y){
  x <- DF[[x]]
  y <- DF[[y]]
  idx <- seq_along(x)[-1]
  as.double( (x[idx] - x[idx-1]) %*% (y[idx] + y[idx-1]) ) / 2
}

library(plyr)

ddply(df,.(Class),trapz,x = "Depth",y = "organismQuantity")
#            Class       V1
#1        Ciliates 136640.1
#2 Dinoflagellates 994945.8
#3     Dinophyceae       NA

数据

df <- read.table(text = "
      Class      Depth   organismQuantity
1   Ciliates        10  1608.89
2   Ciliates        30  2125.09
3   Ciliates        90  1184.92
4   Dinophyceae     10  0.00
5   Dinoflagellates 30  28719.60
6   Dinoflagellates 90  4445.26
",header = TRUE)