【发布时间】:2013-10-02 08:12:35
【问题描述】:
我想测试矩阵的列名是否包含在矩阵的行名中,即 colnames(abun) 是否包含在下面示例中的行名(x)中
abun <- matrix(c(0.4,0,0.6,0.1,0.4,0.5),
nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("x", "y"),
c("A","B","C")))
abun
A B C
x 0.4 0.0 0.6
y 0.1 0.4 0.5
x<-data.frame("Trait1" =c(1,1,0,1),
"Trait2"=c(1,1,1,1),
"Trait3" =c(1,1,0,1),
"Trait4" =c(1,0,1,1))
rownames(x)<-c("A","B","C","D")
x
Trait1 Trait2 Trait3 Trait4
A 1 1 1 1
B 1 1 1 0
C 0 1 0 1
D 1 1 1 1
更新: 我正在编写一个函数,如果行名(x)中不包含列名(abun),我希望抛出一条错误消息。我试过了:
if(colnames(abun) %in% rownames(x) = FALSE)
stop("species names in abun and x do not match")
【问题讨论】: