最近在做多样性分析的时候,代码总是报错,错误为Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘"function"’ to a data.frame,但是之前是可以运行的,数据换了很多格式还是不行,所以请各位大佬指点,到底是啥原因,多谢大佬!
代码如下:
setwd("D:/RDATA")
otu <- read.csv('7samplespearman.csv', row.names = 1, sep = '\t', stringsAsFactors = FALSE, check.names = FALSE)
otu <- t(otu)
library("vegan")
library("picante")
alpha <- function(x, tree = NULL, base = exp(1)) {
est <- estimateR(x)
Richness <- est[1, ]
Chao1 <- est[2, ]
ACE <- est[4, ]
Shannon <- diversity(x, index = 'shannon', base = base)
Simpson <- diversity(x, index = 'simpson')
Pielou <- Shannon / log(Richness, base)
goods_coverage <- 1 - rowSums(x == 1) / rowSums(x)
result <- data.frame(Richness, Shannon, Simpson, Pielou, Chao1, ACE, goods_coverage)
if (!is.null(tree)) {
PD_whole_tree <- pd(x, tree, include.root = FALSE)[1]
names(PD_whole_tree) <- 'PD_whole_tree'
result <- cbind(result, PD_whole_tree)}
result}
write.csv(alpha, 'D:/RDATA/otu_alpha.csv', quote = FALSE)
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘"function"’ to a data.frame