苏华的个人博客分享 http://blog.sciencenet.cn/u/Hunshandake

博文

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

已有 1325 次阅读 2021-12-22 16:25 |个人分类:软件使用|系统分类:科研笔记

数据格式如下:

image.png


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表格中,没能做到,有会做的朋友请留言指教,多谢!



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

上一篇:土壤有效磷的测定建议
下一篇:[转载]Endnote:期刊名称统一设置为缩写
收藏 IP: 159.226.89.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2022-8-18 03:51

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部