如果两个输入的行名和列名不匹配,我想要一个函数停止并返回错误消息。输入可以是矩阵或 data.frames。
我尝试了以下方法但没有成功:
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")))
x<-data.frame("Trait1" =c(1,1,0),"Trait2"=c(1,1,1),
"Trait3" =c(1,1,0),"Trait4" =c(1,0,1))
rownames(x)<-c("A","B","D")
test<-function(abun,x){
if(colnames(abun) != rownames(x))stop("species names in abun and x do not match")
abun<-abun*2
abun
}
test(abun,x)
欢迎任何见解!