rm(list=ls()) #清除R环境中的所有对象
setwd("C:\\Users\\Administrator\\Desktop\\新建文件夹") #设置工作目录
library(vegan)
# 读取数据
df <- read.table("otu_table_R.txt", header = TRUE, row.names = 1, sep = "\t")
# 计算Shannon多样性
shannon_index <- diversity(df)
# 计算Simpson多样性
simpson_index <- diversity(df, "simpson")
# 创建一个新的数据框,包含样本名、Shannon指数和Simpson指数
result <- data.frame(Sample = row.names(df), Shannon = shannon_index, Simpson = simpson_index)
# 打印结果
print(result)
write.table(result, file = "diversity_indices.txt", sep = "\t", row.names = FALSE, quote = FALSE)
标签:Administrator,读取数据,df,library,多样性,计算,alpha From: https://www.cnblogs.com/wzbzk/p/17933069.html