我有一个矩阵。示例数据:
matrix<- matrix(sample(1:10), nrow= 10,ncol=20)
注意:在我的真实矩阵中,行名称是绘图编号,这对于与该行的值保持关联很重要。
我想在矩阵的每一行上执行各种功能(来自纯素包的多样性功能)。功能包括:
library(vegan)
div1<- diversity(matrix, index="shannon")
div2<- diversity(matrix, index="simpson")
div3<- exp(diversity(matrix,index="shannon"))
div4<- 1/(1-diversity(matrix,index="simpson"))
spec.num<- specnumber(matrix)
输出应该是数据帧。该数据帧的每一列代表唯一的多样性指数。输出看起来像:
Rownames div1 div2 div3 div4 spec.num
Plot1 2.995732 0.95 20 20 20
Plot34 2.995732 0.95 20 20 20
Plot56 2.995732 0.95 20 20 20
Plot60 2.995732 0.95 20 20 20
注意:对于我的真实数据,矩阵行对每列都有唯一的值,它们不会像我的示例所示那样重复。因此,div3,div4,spec number的输出不一定与此示例输出中显示的值相同。
答案 0 :(得分:0)
您是否只想查看列绑定结果?
# if you set your sampling range to be 1:1000 these diversity values are no longer constant
mtx <- matrix(sample(1:1000), nrow= 10,ncol=20) # I like to avoid like names
#install.packages("vegan")
library(vegan)
div1<- diversity(mtx, index="shannon")
div2<- diversity(mtx, index="simpson")
div3<- exp(diversity(mtx,index="shannon"))
div4<- 1/(1-diversity(mtx,index="simpson"))
spec.num<- specnumber(mtx)
cbind(div1, div2, div3, div4, spec.num) # cbinding equal length vectors into a data.frame/matrix
#cbind.data.frame()
答案 1 :(得分:0)
library(vegan)
matrix<- matrix(sample(1:10), nrow= 10,ncol=20)
#rownames(matrix) <- paste0("plot", 1:nrow(matrix)) ## this is/was merely an illustration on carrying rownames forward
df <- data.frame(div1=diversity(matrix, index="shannon"), div2=diversity(matrix, index="simpson"),
div3=exp(diversity(matrix,index="shannon")), div4=1/(1-diversity(matrix,index="simpson")), spec.nu=specnumber(matrix), row.names = rownames(matrix))