||
数据格式如下:
library(openxlsx)
library(reshape2)
library(vegan)
herb.data <- read.xlsx("D:/R/XX.xlsx")
#计算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-255662-310032.html),自己发挥了一点分处理计算,最后应该输出到一个Excel表格中,没能做到,有会做的朋友请留言指教,多谢!
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-24 09:41
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社