使用R对每个矩阵元素进行类别重叠分类

时间:2018-01-31 18:16:47

标签: r matrix factory

我正在创建一个函数来对群体矩阵(PPM)的每个元素进行分类。为此,我创建了以下功能。它适用于某些矩阵,但不适用于其他矩阵,我不明白为什么!

我的目标是获得像这样的数组

    [,1]     [,2]     [,3]     [,4]     [,5]    
[1,] "Fert"   "Fert"   "Fert"   "Fert"   "Fert"  
[2,] "Growth" "Stasis" "Retrog" "Retrog" "Retrog"
[3,] "Growth" "Growth" "Stasis" "Retrog" "Retrog"
[4,] "Growth" "Growth" "Growth" "Stasis" "Retrog"
[5,] "Growth" "Growth" "Growth" "Growth" "Stasis"

功能

Categ<-function(x){
#x is your mean or sample population projection matrix
# Stasis
x[diag(x)] <- "Stasis"
# Retrogressions
x[upper.tri(x)] <- "Retrog"
xus
# Growth
x[lower.tri(x)]<-"Growth"
# Fertility 
x[1,]<-"Fert"
return(as.matrix(x))
}

如果

,效果很好
xus<-matrix(1:25,5)
Categ(xus)

但是,如果数组具有“title”

,则不起作用
hudsonia<-structure(c(0.4995, 0.00035, 0, 0, 0, 0, 0, 0, 0.477275, 0, 0, 
0, 4.5782, 0.00335, 0.599, 0.19005, 0, 0, 12.1425, 0.0088, 0.199475, 
0.4679, 0.271475, 0.016175, 22.3167, 0.0162, 0.068725, 0.1246, 
0.545425, 0.2567, 50.1895, 0.036425, 0, 0.069225, 0.17115, 0.759475
), .Dim = c(6L, 6L), .Dimnames = list(c("seed", "seedlings", 
"tiny", "small", "medium", "large"), c("seed", "seedlings", "tiny", 
"small", "medium", "large")))

用法

Categ(hudsonia)

0 个答案:

没有答案