You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用codyn包turnover函数计算物种周转率时遇报错求助

问题描述

在使用R语言codyn包的turnover()函数计算物种时间周转率时,执行以下代码触发报错:

spTurnover <- turnover(df = turnover_dat, 
                         time.var = "year", 
                         species.var = "species", 
                         abundance.var = "abundance",
                       replicate.var = "site")

报错信息:

Error in names(output) <- `*vtmp*` : 
  'names' attribute [1] must be the same length as the vector [0]

数据集前6行的结构如下:

structure(list(species = c("Accipiter badius", "Accipiter badius", 
"Accipiter badius", "Accipiter badius", "Accipiter badius", "Accipiter badius"
), site = c("BVLR", "BVLR", "BVLR", "BVLR", "KKTG", "KKTG"), 
    year = c(1850, 1900, 1950, 2021, 1850, 1900), abundance = c(0, 
    0, 0, 1, 0, 0)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))
报错原因
  1. 物种多样性不足:当前数据集仅包含单一物种Accipiter badius,而周转率的核心是衡量物种的更迭(新物种出现/原有物种消失),单一物种无法产生有效周转数据,导致函数计算返回空结果。
  2. 有效丰度记录缺失:除BVLR样地2021年的记录外,其余所有行的丰度均为0,没有足够的物种存在/变化的时间配对数据,函数内部计算时生成了长度为0的向量,却试图为其分配名称,最终触发不匹配错误。
  3. 样地数据无效:KKTG样地的所有时间点丰度均为0,无任何可用于计算的物种存在记录,无法参与周转率分析。
解决步骤
  • 补充多物种数据:确保数据集中包含至少两个不同物种,且每个样地(site)的不同时间点(year)存在物种丰度的变化(如物种出现、消失或丰度波动),这是计算周转率的基础。
  • 清理无效记录:可以先过滤掉丰度为0的行,减少无效数据干扰:
# 加载dplyr用于数据过滤
library(dplyr)
turnover_dat_clean <- turnover_dat %>% filter(abundance > 0)

若过滤后数据量仍不足以支持计算,需补充更多有效样地-时间-物种的丰度记录。

  • 校验样地时间配对:确保每个site对应的year至少有两个时间点存在非零丰度的物种,否则该样地无法参与周转率计算,可提前筛选出符合条件的样地:
# 筛选出至少有两个时间点有非零丰度的样地
valid_sites <- turnover_dat %>%
  filter(abundance > 0) %>%
  group_by(site) %>%
  summarise(n_time = n_distinct(year)) %>%
  filter(n_time >= 2) %>%
  pull(site)

turnover_dat_valid <- turnover_dat %>% filter(site %in% valid_sites)

内容的提问来源于stack exchange,提问作者Vijay Ramesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 08:12:14