测试col名称是否在R中的行名称中 [英] Test if col names are in row names in R
本文介绍了测试col名称是否在R中的行名称中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的示例中,我想测试矩阵的列名是否包含在矩阵的行名内,即colnames(abun)是否包含在行名(x)之内
I would like to test if the column names of a matrix are contained within the rownames of a matrix i.e. if colnames(abun) and contained within rownames(x) in example below
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
更新: 我正在编写一个函数,并且如果rownames(x)中不包含colnames(abun),则会引发一条错误消息.我已经尝试过:
UPDATE: I am writing a function and would like an error message to be thrown if a colnames(abun) is not contained within rownames(x). I have tried:
if(colnames(abun) %in% rownames(x) = FALSE)
stop("species names in abun and x do not match")
推荐答案
colnames(abun)[
colnames(abun) %in% rownames(x)
]
colnames(abun) %in% rownames(x)
返回一个true/false向量,指示rownames(x)
中存在colnames(abun)
上的哪个元素.
colnames(abun) %in% rownames(x)
returns a true/false vector indicating which element on colnames(abun)
is present in rownames(x)
.
这篇关于测试col名称是否在R中的行名称中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文