# R语言笔记，计算物种多样性

library(openxlsx)

library(reshape2)

library(vegan)

#计算Pr1多样性

pr1<-herb.data[1:13,1:4]

herb.mat <- acast(pr1,

formula = repeatname ~ species ,

value.var = "abundance",

fill = 0)

Shannon.Wiener <- diversity(herb.mat, index = "shannon")

Simpson <- diversity(herb.mat, index = "simpson")

S <- specnumber(herb.mat)

plot(S)

J <- Shannon.Wiener/log(S)

hdataexport <- rbind(Shannon.Wiener,Simpson,S,J)

write.csv(hdataexport, file = "D:/R/XX R.csv")

#将结果剪切到新的excel并标注Pr1

#计算Pr2多样性

pr2<-herb.data[14:24,1:4]

herb.mat <- acast(pr2,

formula = repeatname ~ species ,

value.var = "abundance",

fill = 0)

Shannon.Wiener <- diversity(herb.mat, index = "shannon")

Simpson <- diversity(herb.mat, index = "simpson")

S <- specnumber(herb.mat)

plot(S)

J <- Shannon.Wiener/log(S)

hdataexport <- rbind(Shannon.Wiener,Simpson,S,J)

write.csv(hdataexport, file = "D:/R/XX R.csv")

#将结果剪切到新的excel并标注Pr2

#计算Pr3多样性

pr3<-herb.data[25:34,1:4]

herb.mat <- acast(pr3,

formula = repeatname ~ species ,

value.var = "abundance",

fill = 0)

Shannon.Wiener <- diversity(herb.mat, index = "shannon")

Simpson <- diversity(herb.mat, index = "simpson")

S <- specnumber(herb.mat)

plot(S)

J <- Shannon.Wiener/log(S)

hdataexport <- rbind(Shannon.Wiener,Simpson,S,J)

write.csv(hdataexport, file = "D:/R/XX R.csv")

#将结果剪切到新的excel并标注Pr3

#计算Pa多样性

pa<-herb.data[35:51,1:4]

herb.mat <- acast(pa,

formula = repeatname ~ species ,

value.var = "abundance",

fill = 0)

Shannon.Wiener <- diversity(herb.mat, index = "shannon")

Simpson <- diversity(herb.mat, index = "simpson")

S <- specnumber(herb.mat)

plot(S)

J <- Shannon.Wiener/log(S)

hdataexport <- rbind(Shannon.Wiener,Simpson,S,J)

write.csv(hdataexport, file = "D:/R/XX R.csv")

https://blog.sciencenet.cn/blog-587128-1317707.html

## 全部精选博文导读

GMT+8, 2024-4-19 20:24