Я пытаюсь присвоить средние значения из таблицы данных (Ecoli) в треугольную матрицу (Ecoli_matrix) с помощью цикла for. Однако, когда цикл достигает индексов 0,6 и 0,9, я получаю NaN вместо соответствующего значения.
Я пробовал разные варианты цикла for и проверял, чтобы все значения были числовыми.
Ecoli_matrix <- matrix(0, nrow = 8, ncol = 8)
rownames(Ecoli_matrix) <- seq(0.3,1.0,0.1)
colnames(Ecoli_matrix) <- seq(0.3,1.0,0.1)
Ecoli_matrix[lower.tri(Ecoli_matrix)] <- NA
nums <- as.numeric(seq(0.3,1.0,0.1))
for (i in 1:8) {
a <- nums[i]
for (j in 1:8) {
b <- nums[j]
if (!is.na(Ecoli_matrix[i,j])) {
Ecoli_matrix[i,j] <- mean(Ecoli[completeness_1 == a & completeness_2 == b]$ANI)
}else{
next
}
}
}
Ecoli_matrix
Ожидаемые результаты:
реальные средние значения вместо NaN
0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0.3 98.53066 98.45858 98.98962 NaN 99.25422 99.64324 NaN 100
0.4 NA 98.87488 99.03502 NaN 99.34984 99.74074 NaN 100
0.5 NA NA 99.31126 NaN 99.57964 99.72836 NaN 100
0.6 NA NA NA NaN NaN NaN NaN NaN
0.7 NA NA NA NA 99.71534 99.85268 NaN 100
0.8 NA NA NA NA NA 99.90262 NaN 100
0.9 NA NA NA NA NA NA NaN NaN
1 NA NA NA NA NA NA NA 100
Таблица Ecoli:
completeness_1 completeness_2 ANI
0.3 0.3 97.9607
0.3 0.4 98.8787
0.3 0.5 99.2944
0.3 0.6 98.8600
0.3 0.7 99.1515
0.3 0.8 99.7849
0.3 0.9 99.5993
0.3 1.0 100.0000
0.3 0.3 98.4126
0.3 0.4 98.3872
0.3 0.5 98.5145
0.3 0.6 99.4992
0.3 0.7 99.4004
0.3 0.8 99.6906
0.3 0.9 99.8292
0.3 1.0 100.0000
0.3 0.3 99.1003
0.3 0.4 98.4184
0.3 0.5 99.3127
0.3 0.6 98.8290
0.3 0.7 99.3498
0.3 0.8 99.6751
0.3 0.9 99.8596
0.3 1.0 100.0000
0.3 0.3 98.6554
0.3 0.4 97.9706
0.3 0.5 98.5052
0.3 0.6 99.3553
0.3 0.7 99.2609
0.3 0.8 99.4636
0.3 0.9 99.7513
0.3 1.0 100.0000
0.3 0.3 98.5243
0.3 0.4 98.6380
0.3 0.5 99.3213
0.3 0.6 99.2216
0.3 0.7 99.1085
0.3 0.8 99.6020
0.3 0.9 99.8939
0.3 1.0 100.0000
0.4 0.3 98.9680
0.4 0.4 99.1399
0.4 0.5 99.0710
0.4 0.6 99.1930
0.4 0.7 99.5270
0.4 0.8 99.7888
0.4 0.9 99.8802
0.4 1.0 100.0000
0.4 0.3 98.1257
0.4 0.4 98.4355
0.4 0.5 98.7498
0.4 0.6 99.2367
0.4 0.7 99.2529
0.4 0.8 99.7091
0.4 0.9 99.7455
0.4 1.0 100.0000
0.4 0.3 98.5192
0.4 0.4 98.9832
0.4 0.5 99.1183
0.4 0.6 99.2549
0.4 0.7 99.0702
0.4 0.8 99.7385
0.4 0.9 99.8020
0.4 1.0 100.0000
0.4 0.3 98.8609
0.4 0.4 99.1154
0.4 0.5 99.2910
0.4 0.6 99.5090
0.4 0.7 99.3003
0.4 0.8 99.6992
0.4 0.9 99.7460
0.4 1.0 100.0000
0.4 0.3 98.7781
0.4 0.4 98.7004
0.4 0.5 98.9450
0.4 0.6 99.2914
0.4 0.7 99.5988
0.4 0.8 99.7681
0.4 0.9 99.8564
0.4 1.0 100.0000
0.5 0.3 99.1444
0.5 0.4 99.3652
0.5 0.5 99.6018
0.5 0.6 99.5122
0.5 0.7 99.7143
0.5 0.8 99.6552
0.5 0.9 99.8233
0.5 1.0 100.0000
0.5 0.3 99.1689
0.5 0.4 99.3622
0.5 0.5 99.2013
0.5 0.6 99.4480
0.5 0.7 99.7191
0.5 0.8 99.7735
0.5 0.9 99.8405
0.5 1.0 100.0000
0.5 0.3 98.7547
0.5 0.4 99.0458
0.5 0.5 99.1784
0.5 0.6 99.5908
0.5 0.7 99.3255
0.5 0.8 99.6829
0.5 0.9 99.8085
0.5 1.0 100.0000
0.5 0.3 98.8526
0.5 0.4 99.4464
0.5 0.5 99.1989
0.5 0.6 99.6929
0.5 0.7 99.6149
0.5 0.8 99.8013
0.5 0.9 99.8748
0.5 1.0 100.0000
0.5 0.3 99.0085
0.5 0.4 98.3852
0.5 0.5 99.3759
0.5 0.6 99.2256
0.5 0.7 99.5244
0.5 0.8 99.7289
0.5 0.9 99.9002
0.5 1.0 100.0000
0.6 0.3 98.8855
0.6 0.4 99.1283
0.6 0.5 99.3423
0.6 0.6 99.3840
0.6 0.7 99.6955
0.6 0.8 99.7013
0.6 0.9 99.7296
0.6 1.0 100.0000
0.6 0.3 99.0861
0.6 0.4 99.5717
0.6 0.5 99.4429
0.6 0.6 99.5169
0.6 0.7 99.7044
0.6 0.8 99.9558
0.6 0.9 99.7909
0.6 1.0 100.0000
0.6 0.3 99.3989
0.6 0.4 99.4521
0.6 0.5 99.5094
0.6 0.6 99.6623
0.6 0.7 99.6799
0.6 0.8 99.8384
0.6 0.9 99.9410
0.6 1.0 100.0000
0.6 0.3 99.1774
0.6 0.4 99.2779
0.6 0.5 99.4602
0.6 0.6 99.4447
0.6 0.7 99.5277
0.6 0.8 99.7600
0.6 0.9 99.7568
0.6 1.0 100.0000
0.6 0.3 99.6414
0.6 0.4 99.3121
0.6 0.5 99.6987
0.6 0.6 99.6514
0.6 0.7 99.6481
0.6 0.8 99.9006
0.6 0.9 99.9636
0.6 1.0 100.0000
0.7 0.3 99.4547
0.7 0.4 99.6649
0.7 0.5 99.6927
0.7 0.6 99.5586
0.7 0.7 99.7189
0.7 0.8 99.9507
0.7 0.9 99.8915
0.7 1.0 100.0000
0.7 0.3 99.7365
0.7 0.4 99.5755
0.7 0.5 99.4170
0.7 0.6 99.6824
0.7 0.7 99.6474
0.7 0.8 99.8268
0.7 0.9 99.9137
0.7 1.0 100.0000
0.7 0.3 99.6652
0.7 0.4 99.6749
0.7 0.5 99.6315
0.7 0.6 99.6506
0.7 0.7 99.7673
0.7 0.8 99.7651
0.7 0.9 99.8527
0.7 1.0 100.0000
0.7 0.3 99.2376
0.7 0.4 99.5145
0.7 0.5 99.5446
0.7 0.6 99.5321
0.7 0.7 99.6898
0.7 0.8 99.7904
0.7 0.9 99.8675
0.7 1.0 100.0000
0.7 0.3 99.3614
0.7 0.4 99.4177
0.7 0.5 99.5746
0.7 0.6 99.6963
0.7 0.7 99.7533
0.7 0.8 99.9304
0.7 0.9 99.8797
0.7 1.0 100.0000
0.8 0.3 99.7806
0.8 0.4 99.7076
0.8 0.5 99.7752
0.8 0.6 99.6750
0.8 0.7 99.8162
0.8 0.8 99.8961
0.8 0.9 99.9088
0.8 1.0 100.0000
0.8 0.3 99.8302
0.8 0.4 99.6818
0.8 0.5 99.7987
0.8 0.6 99.7489
0.8 0.7 99.8682
0.8 0.8 99.8810
0.8 0.9 99.8724
0.8 1.0 100.0000
0.8 0.3 99.4582
0.8 0.4 99.6642
0.8 0.5 99.7310
0.8 0.6 99.7085
0.8 0.7 99.8668
0.8 0.8 99.8890
0.8 0.9 99.9545
0.8 1.0 100.0000
0.8 0.3 99.4662
0.8 0.4 99.5455
0.8 0.5 99.7426
0.8 0.6 99.8504
0.8 0.7 99.7353
0.8 0.8 99.9202
0.8 0.9 99.9057
0.8 1.0 100.0000
0.8 0.3 99.7896
0.8 0.4 99.7198
0.8 0.5 99.9195
0.8 0.6 99.7826
0.8 0.7 99.8543
0.8 0.8 99.9268
0.8 0.9 99.9266
0.8 1.0 100.0000
0.9 0.3 99.9185
0.9 0.4 99.8778
0.9 0.5 100.0000
0.9 0.6 99.9429
0.9 0.7 99.8414
0.9 0.8 99.9964
0.9 0.9 100.0000
0.9 1.0 100.0000
0.9 0.3 99.8731
0.9 0.4 99.7531
0.9 0.5 99.8569
0.9 0.6 99.8611
0.9 0.7 99.9492
0.9 0.8 99.9215
0.9 0.9 99.9877
0.9 1.0 100.0000
0.9 0.3 99.9271
0.9 0.4 99.8416
0.9 0.5 99.7653
0.9 0.6 99.8329
0.9 0.7 99.9639
0.9 0.8 100.0000
0.9 0.9 99.9718
0.9 1.0 100.0000
0.9 0.3 99.9071
0.9 0.4 99.9764
0.9 0.5 99.9424
0.9 0.6 99.9093
0.9 0.7 100.0000
0.9 0.8 99.9932
0.9 0.9 99.9669
0.9 1.0 100.0000
0.9 0.3 99.7999
0.9 0.4 99.8924
0.9 0.5 99.8942
0.9 0.6 99.9287
0.9 0.7 99.9513
0.9 0.8 99.9862
0.9 0.9 100.0000
0.9 1.0 100.0000
1.0 0.3 100.0000
1.0 0.4 100.0000
1.0 0.5 100.0000
1.0 0.6 100.0000
1.0 0.7 100.0000
1.0 0.8 100.0000
1.0 0.9 100.0000
1.0 1.0 100.0000
1.0 0.3 100.0000
1.0 0.4 100.0000
1.0 0.5 100.0000
1.0 0.6 100.0000
1.0 0.7 100.0000
1.0 0.8 100.0000
1.0 0.9 100.0000
1.0 1.0 100.0000
1.0 0.3 100.0000
1.0 0.4 100.0000
1.0 0.5 100.0000
1.0 0.6 100.0000
1.0 0.7 100.0000
1.0 0.8 100.0000
1.0 0.9 100.0000
1.0 1.0 100.0000
1.0 0.3 100.0000
1.0 0.4 100.0000
1.0 0.5 100.0000
1.0 0.6 100.0000
1.0 0.7 100.0000
1.0 0.8 100.0000
1.0 0.9 100.0000
1.0 1.0 100.0000
1.0 0.3 100.0000
1.0 0.4 100.0000
1.0 0.5 100.0000
1.0 0.6 100.0000
1.0 0.7 100.0000
1.0 0.8 100.0000
1.0 0.9 100.0000
1.0 1.0 100.0000