在 R 中使用 cbin() 时的警告消息

Warning message when using cbin() in R

我有一个匹配捐赠者和接受者血型的功能(接受者A --->捐赠者A或O; 接受者 B ---> 捐赠者 B 或 O; 接受者 O ---> 捐赠者 O; 受体 AB ---> 供体 A、B、O 或 AB)。即使我获得了所需的输出,我仍不断收到警告消息。

1- 这是我的数据

######################
#  Sample data       #
######################
# sample data for recipients

IDr= c(seq(1,5))
BTR=c("A","B","AB","O","O")
data_R=data.frame(IDr,BTR,A=c(0,1,rep(0,3)),B=c(0,rep(0,3),1),C=c(0,rep(1,3),0),D=c(0,rep(1,4)),E=c(1,1,0,rep(1,1),0),stringsAsFactors=FALSE)

data_R
  IDr BTR A B C D E
1   1   A 0 0 0 0 1
2   2   B 1 0 1 1 1
3   3  AB 0 0 1 1 0
4   4   O 0 0 1 1 1
5   5   O 0 1 0 1 0

# sample data for donors

IDd= c(seq(1,8))
BTD= c("A","B","AB","O","AB","AB","O","O")
fg= c(rep(0.0025, each=2),rep(0.00125, each=2),rep(0.0011, each=2),rep(0.0015, each=2))
data_D=data.frame(IDd,BTD,A=c(rep(0,5),1,1,1),B=c(rep(0,6),1,1),C=c(rep(1,7),0),D=rep(1,8),E=c(rep(0,5),rep(1,2),0),fg,stringsAsFactors=FALSE)

# i ordered my data_D
data_D
  IDd BTD A B C D E      fg
2   2   A 0 0 1 1 0 0.00250
4   4  AB 0 0 1 1 0 0.00125
5   5   B 0 0 1 1 0 0.00110
6   6   O 0 0 1 1 0 0.00110
7   7  AB 0 0 1 1 0 0.00150
8   8  AB 1 0 1 1 1 0.00150
1   1   O 1 1 0 1 0 0.00250
3   3   O 1 1 1 1 1 0.00125

2- 这是我匹配血型的函数

# my function
  ftest=function(i){
    if(data_R[i,2]=="A"){
      tab=as.data.frame(cbind(data_R[i,1:2],data_D[which((data_D[2]=="A") | (data_D[2]=="O")),][,1:2]))
    }else if(data_R[i,2]=="B"){
      tab=as.data.frame(cbind(data_R[i,1:2],data_D[which((data_D[2]=="B") | (data_D[2]=="O")),][,1:2]))
    }else if(data_R[i,2]=="O"){
      tab=as.data.frame(cbind(data_R[i,1:2],data_D[which (data_D[2]=="O"),][,1:2]))
    }else{
      tab=as.data.frame(cbind(data_R[i,1:2],data_D[,1:2]))
    }
   return(tab)
  }

#  output
 ftest(1)
  IDr BTR IDd BTD
1   1   A   2   A
2   1   A   6   O
3   1   A   1   O
4   1   A   3   O

# Warning message:
# In data.frame(..., check.names = FALSE) :
#  row names were found from a short variable and have been discarded

您知道如何避免出现此警告消息吗?任何建议将不胜感激。

提前致谢。

嘿,我认为你应该在你的 cbind 中尝试 row.names = NULL。这是您的代码中的示例

tab=as.data.frame(cbind(data_R[i,1:2],data_D[,1:2], row.names = NULL))