R语言计算生物多样性指数
程序员文章站
2024-03-03 16:36:52
...
R语言计算生物多样性指数
计算生物多样性
rm(list = ls())
setwd("D:/R Working Directory")
getwd()
library(readxl)
library(vegan)
library(reshape2)
fyzw <- read_excel("C:/Users/Administrator/Desktop/LQ20200104.xlsx",
sheet = "FYZW", range = "A1:G224")
fydw<-fydw[,c(1,4,6)] ## 数据在1列、4列和6列中
colnames(fydw)<-c("plotname","species","abundance")##也可以改为其他命名
herb<-acast(fydw,formula = plotname~species,
fun.aggregate = sum,value.var = "abundance")##转换成物种矩阵调用reshape2包
#计算Shannon-Wiener指数
Shannon.Wiener <-diversity(herb, index = "shannon")###调用vegan包
#计算Simpson指数
Simpson <- diversity(herb, index = "simpson")
#计算Inverse Simpson指数
Inverse.Simpson <- diversity(herb, index = "inv")
#计算物种累计数
S <- specnumber(herb)##物种数
plot(S)
#计算Pielou均匀度指数
J <- Shannon.Wiener/log(S)
c<-data.frame(cbind(Shannon.Wiener,J ))
print(c)##打印数据