R:计算alpha多样性指数
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)