iNext包incidence_raw数据格式报错:Error in yi[i]:(nT - 1) : NA/NaN argument
解决iNext/estimateD处理存在/缺失矩阵的报错问题
问题根源
你之前的错误出在两个地方:
- 数据转换方式错误:
as.list(data.frame)会把数据框的每一列转成列表元素(每个元素是单个位点的物种存在向量),但iNext要求incidence_raw格式的输入是物种×采样单元的矩阵(单个群落),或由多个这类矩阵组成的列表(多个群落)。 - 可能存在NA值:报错里的
NA/NaN argument提示数据中存在缺失值,这会打断函数计算。
正确处理步骤
假设你的原始数据框名为sp_site_df(24行=物种,6列=位点,1=存在,0=不存在):
1. 清理数据(必做)
先检查并替换NA值:
# 将所有NA替换为0(存在/缺失数据无NA意义) sp_site_df[is.na(sp_site_df)] <- 0 # 转成矩阵(可选,数据框也可直接用) sp_site_mat <- as.matrix(sp_site_df)
2. 根据研究目的选择输入格式
场景1:所有6个位点属于同一个群落(分析该群落的多样性)
直接用物种×位点的矩阵作为输入,指定datatype="incidence_raw":
library(iNEXT) # 运行iNext,q指定多样性阶数(0=物种丰富度,1=Shannon,2=Simpson) inext_result <- iNext(sp_site_mat, datatype = "incidence_raw", q = c(0,1,2)) # 运行estimateD(比如基于覆盖度标准化) estd_result <- estimateD(sp_site_mat, datatype = "incidence_raw", base = "coverage", level = 0.9)
场景2:每个位点是独立的群落(比较6个位点的多样性)
需要把每个位点转成单独的物种×1矩阵,再组成列表:
# 生成由6个物种×1矩阵组成的列表 assemblage_list <- lapply(colnames(sp_site_df), function(site) { # 提取单个位点的数据并转成矩阵 site_mat <- as.matrix(sp_site_df[, site, drop=FALSE]) site_mat }) # 给列表元素命名(对应位点名称) names(assemblage_list) <- colnames(sp_site_df) # 运行iNext inext_result <- iNext(assemblage_list, datatype = "incidence_raw", q = c(0,1,2)) # 运行estimateD estd_result <- estimateD(assemblage_list, datatype = "incidence_raw", base = "sample", level = 6)
验证输入格式
可以用以下代码检查列表中的元素是否符合要求:
# 检查第一个元素的维度(应该是24行×1列) dim(assemblage_list[[1]])
内容的提问来源于stack exchange,提问作者Drips
相关产品推荐
相关产品推荐

