上下文:我正在尝试在Shiny中创建一个算法来比较用户提交的一组捐赠者和收件人的HLA类型,但我的代码由于NAs而失败,
输入:
> recipss
Recip_ID HLA.A HLA.B HLA.C HLA.DRB1 HLA.DQB1 HLA.DQA1
1 1 A*02:01 B*44:02 C*03:02 DRB1*13:02 DQB1*03:01 DQA1*03:02
2 2 A*02:05 B*44:03 C*02:02 DRB1*13:03 DQB1*02:02 DQA1*04:01
> donorss
Donor_ID HLA.A HLA.B HLA.C HLA.DRB1 HLA.DQB1 HLA.DQA1
1 11 A*01:01 B*40:01 C*01:02 DRB1*12:01 DQB1*02:01 DQA1*01:01
2 12 A*02:01 B*40:02 C*02:02 DRB1*12:02 DQB1*02:02 DQA1*01:02
代码:
allele_mismatches_donors = apply(donorss,1,function(x){apply(recipss,1,function(y){x[which(x!=y)]})})
allele_mismatches_recips = apply(recipss,1,function(x){apply(donorss,1,function(y){x[which(x!=y)]})})
输出:
> allele_mismatches_donors
$`1`
1 2
Donor_ID "11" "11"
HLA.A "A*01:01" "A*01:01"
HLA.B "B*40:01" "B*40:01"
HLA.C "C*01:02" "C*01:02"
HLA.DRB1 "DRB1*12:01" "DRB1*12:01"
HLA.DQB1 "DQB1*02:01" "DQB1*02:01"
HLA.DQA1 "DQA1*01:01" "DQA1*01:01"
$`2`
$`2`$`1`
Donor_ID HLA.B HLA.C HLA.DRB1 HLA.DQB1 HLA.DQA1
"12" "B*40:02" "C*02:02" "DRB1*12:02" "DQB1*02:02" "DQA1*01:02"
$`2`$`2`
Donor_ID HLA.A HLA.B HLA.DRB1 HLA.DQA1
"12" "A*02:01" "B*40:02" "DRB1*12:02" "DQA1*01:02"
> allele_mismatches_recips
$`1`
$`1`$`1`
Recip_ID HLA.A HLA.B HLA.C HLA.DRB1 HLA.DQB1 HLA.DQA1
"1" "A*02:01" "B*44:02" "C*03:02" "DRB1*13:02" "DQB1*03:01" "DQA1*03:02"
$`1`$`2`
Recip_ID HLA.B HLA.C HLA.DRB1 HLA.DQB1 HLA.DQA1
"1" "B*44:02" "C*03:02" "DRB1*13:02" "DQB1*03:01" "DQA1*03:02"
$`2`
$`2`$`1`
Recip_ID HLA.A HLA.B HLA.C HLA.DRB1 HLA.DQB1 HLA.DQA1
"2" "A*02:05" "B*44:03" "C*02:02" "DRB1*13:03" "DQB1*02:02" "DQA1*04:01"
$`2`$`2`
Recip_ID HLA.A HLA.B HLA.DRB1 HLA.DQA1
"2" "A*02:05" "B*44:03" "DRB1*13:03" "DQA1*04:01"
问题:
> allele_mismatches_donors[[1]][[1]]
[1] "11"
> allele_mismatches_recips[[1]][[1]]
Recip_ID HLA.A HLA.B HLA.C HLA.DRB1 HLA.DQB1 HLA.DQA1
"1" "A*02:01" "B*44:02" "C*03:02" "DRB1*13:02" "DQB1*03:01" "DQA1*03:02"
问题在于,尽管变量是完全相同的结构(我认为)并且包含相同类型的数据,但嵌套的apply语句输出的变量具有不相同的结构,这些变量无用,因为我试图抓住sub -elements对每个变量使用相同的过程。