我有一个包含 120 个调查点的数据集,每个调查点一年调查 4 次(总共 480 个调查)。我们在 15 分钟的调查中每隔 3 分钟计算一次物种的存在或不存在。我感兴趣的是确定每增加 3 分钟的时间间隔(又称为物种积累),就会观察到多少个物种。
我尝试使用 vegan 来做物种积累曲线,并且我已经弄清楚如何将 specaccum 函数应用于多个点。这是我的代码:
# Loading packages
library(vegan)
# setwd first
Toronto_data=read.csv(file.choose())
# Converting columns that won't be used in the species accumulation curve into factors
cols_to_convert <- c("SurveyDay", "TimeBlock")
Toronto_data[cols_to_convert] <- lapply(Toronto_data[cols_to_convert], as.factor)
# Split the data frame by multiple factors: Point ID and Number of Days Survey Occurred (~120 points x 4 survey dates)
list_of_samples <- split(Toronto_data, interaction(Toronto_data$PointID, Toronto_data$SurveyDay))
# Remove empty data frames from split_data
list_of_samples <- list_of_samples[sapply(list_of_samples, nrow) > 0]
# Create function to apply specaccum function to numeric columns. Want to use "collector" rather than "exact but it will not work.
specaccum_function <- function(data) {
# Ensure that only numeric columns are passed
numeric_data <- data[sapply(data, is.numeric)]
# Check if numeric_data has more than one row and one column
if (nrow(numeric_data) > 1 && ncol(numeric_data) > 1) {
return(specaccum(numeric_data, "collector"))
} else {
stop("Not enough data to calculate species accumulation.")
}
}
# Apply specaccum to each subset
results <- lapply(list_of_samples, function(list_of_samples) {
specaccum_function(list_of_samples)
})
但是我相信我需要使用“收集器”方法,当我这样做时,我不断收到相同的错误“
Error in rowSums(apply(x[ind, , drop = FALSE], 2, cumsum) > 0) :
'x' must be an array of at least two dimensions
查看我的代码创建的列表,列表中的所有对象似乎都是 5 x76 的数据帧,超过二维,所以我无法弄清楚问题是什么。
来自 dput 的数据子集在这里:
structure(list(PointID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), SurveyDay = c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L), TimeBlock = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), SpeciesA = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), SpeciesB = c(1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L), SpeciesC = c(0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), SpeciesD = c(1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), SpeciesE = c(0L,
0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), SpeciesF = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA,
-20L))
任何提供的帮助将不胜感激!
我假设您的
dput
中的数据应该是 Toronto_data
。 当我运行它时,我只收到部分数据的错误。 也就是说,
specaccum_function(list_of_samples$`2.2`)
给了我错误。 但那是因为
list_of_samples$`2.2`
只有一行数据。 如果将
stop()
更改为 warning()
,您将看到列表中的其他元素毫无问题地返回。