|||
导入的数据格式如下:
R语言读取数据和计算方法:
library(readxl)
hdata <- read_excel(path = "bcdherb.xlsx", sheet = 1, col_names = T)
hdata <- hdata[,2:91]
#循环计算H值
i <- 1
totalcol <- ncol(hdata)
h <- vector(length = totalcol)
D <- vector(length = totalcol)
J <- vector(length = totalcol)
N <- vector(length = totalcol)
for(i in i:totalcol){
a <- names(hdata)[i] #提取数据框表头
tempdata <- hdata[[a]] #提取数据框中表头为a的数据Pi
tempdata <- na.omit(tempdata)
h[i] <- -1*sum(tempdata*log(tempdata)) #H值计算Shannon-Wiener多样性指数
D[i] <- 1-sum(tempdata^2) #Simpson指数
J[i] <- h[i]/log(length(tempdata)) #Pielou均匀度指数
N[i] <- length(tempdata) #有效值个数
}
h
D
J
N
hdataexport <- rbind(hdata,h,D,J,N)
write.csv(hdataexport, file = "bcdherbExport.csv")
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-27 04:13
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社