【问题标题】:Warning message when using cbin() in R在 R 中使用 cbin() 时的警告消息
【发布时间】:2023-03-14 14:52:01
【问题描述】:

我有一个匹配供体和受体血型的功能(受体 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

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

提前谢谢你。

【问题讨论】:

  • 您好,欢迎来到 SO。尝试以这种格式提出您的问题。 1) 你拥有的数据,2) 你想做什么 3) 你期望什么 4) 你尝试过什么
  • @Onyambu 好的,正在努力。谢谢!

标签: r if-statement vector cbind


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-20
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2018-07-24
    • 2012-02-09
    相关资源
    最近更新 更多