首页 > 解决方案 > 深度整合物种丰度的整合功能

问题描述

嘿,

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

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)

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

     Class       V1
1        Ciliates 136640.1
2 Dinoflagellates       NA
3     Dinophyceae      0.0

Warning messages:
1: In averages * diff(x$Depth) :
  longer object length is not a multiple of shorter object length

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

标签: rintegrate

解决方案


这是一种使用trapzpackage中的函数的方法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)

推荐阅读