我将igraph包与R中的有向图一起使用,但是即使图正确连接,子组件函数也不会返回顶点:
例如,我想获得可以到达顶点47的所有顶点。如果我打印图形,则有51 ->47
和55 ->47
边:
IGRAPH d77ba4d DN-- 672 671 --
+ attr: name (v/c)
+ edges from d77ba4d (vertex names):
[1] 1 ->2 2 ->0 3 ->2 4 ->3 5 ->4 6 ->5 7 ->4 8 ->9 9 ->5
[10] 10 ->13 11 ->14 12 ->17 13 ->17 14 ->13 15 ->14 16 ->20 18 ->11 19 ->9
[19] 20 ->19 21 ->11 22 ->19 23 ->22 24 ->25 25 ->22 26 ->23 27 ->25 28 ->26
[28] 29 ->23 30 ->28 31 ->26 32 ->34 33 ->34 34 ->37 35 ->28 36 ->37 37 ->35
[37] 38 ->39 39 ->33 40 ->33 41 ->38 42 ->40 43 ->38 44 ->43 45 ->43 46 ->45
[46] 47 ->41 48 ->40 49 ->45 50 ->31 51 ->47 52 ->51 496->492 53 ->35 54 ->52
[55] 55 ->47 56 ->55 57 ->63 58 ->50 59 ->58 60 ->59 61 ->50 62 ->55 63 ->62
[64] 64 ->62 65 ->64 66 ->58 67 ->63 68 ->64 69 ->60 70 ->60 71 ->68 72 ->69
+ ... omitted several edges
但是当我调用子组件函数时,会得到一个唯一的错误顶点:
subcomponent(g, 47, mode = "in")
+ 1/672 vertex, named, from d77ba4d:
[1] 48
它可能来自哪里?这是使我烦恼的数据:
vertices <- as.data.frame(SUB = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 496, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 17))
edges <- as.data.frame(FROM_NODE = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 496, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 17),
TO_NODE = c(2, 0, 2, 3, 4, 5, 4, 9, 5, 13, 14, 17, 17, 13, 14, 20, 11, 9, 19, 11, 19, 22, 25, 22, 23, 25, 26, 23, 28, 26, 34, 34, 37, 28, 37, 35, 39, 33, 33, 38, 40, 38, 43, 43, 45, 41, 40, 45, 31, 47, 51, 492, 35, 52, 47, 55, 63, 50, 58, 59, 50, 55, 62, 62, 64, 58, 63, 64, 60, 60, 68, 69, 68, 73, 69, 80, 73, 77, 77, 79, 79, 80, 81, 81, 84, 84, 86, 52, 143, 143, 86, 88, 91, 100, 100, 88, 91, 97, 109, 106, 109, 101, 106, 101, 117, 117, 97, 112, 112, 107, 110, 116, 110, 59, 116, 127, 127, 107, 113, 119, 120, 120, 122, 119, 122, 125, 129, 129, 148, 125, 130, 161, 134, 161, 134, 144, 144, 96, 138, 149, 149, 130, 166, 166, 142, 154, 154, 207, 207, 145, 145, 164, 142, 164, 150, 155, 214, 196, 162, 163, 160, 160, 165, 196, 168, 177, 169, 169, 171, 174, 174, 181, 155, 173, 162, 175, 186, 181, 178, 179, 177, 178, 173, 182, 189, 189, 175, 171, 191, 163, 197, 173, 192, 187, 184, 208, 208, 197, 192, 187, 194, 150, 215, 179, 201, 205, 206, 206, 204, 138, 215, 195, 113, 204, 219, 205, 195, 184, 210, 193, 216, 165, 214, 220, 194, 168, 216, 221, 193, 231, 212, 228, 210, 212, 244, 229, 228, 219, 244, 239, 201, 182, 264, 264, 239, 231, 264, 234, 238, 242, 249, 251, 221, 264, 253, 249, 252, 252, 258, 242, 260, 234, 260, 263, 263, 253, 257, 258, 261, 257, 268, 270, 272, 271, 274, 297, 297, 191, 261, 278, 265, 271, 268, 272, 284, 283, 288, 280, 265, 289, 293, 274, 303, 293, 294, 294, 296, 296, 298, 299, 290, 301, 301, 288, 314, 278, 306, 299, 308, 308, 310, 286, 284, 313, 314, 311, 316, 317, 328, 328, 320, 321, 322, 292, 289, 334, 334, 327, 322, 329, 330, 330, 332, 317, 332, 325, 286, 312, 298, 341, 304, 335, 342, 325, 313, 345, 310, 342, 348, 336, 336, 329, 348, 353, 304, 335, 350, 290, 358, 371, 341, 371, 339, 312, 345, 355, 320, 357, 367, 348, 353, 321, 370, 364, 357, 370, 355, 376, 358, 379, 378, 316, 364, 373, 365, 377, 398, 383, 373, 292, 398, 327, 380, 389, 386, 395, 385, 397, 386, 380, 394, 385, 376, 378, 403, 400, 389, 392, 407, 400, 407, 394, 414, 306, 410, 410, 350, 311, 367, 412, 397, 395, 421, 392, 422, 365, 432, 427, 412, 429, 432, 423, 417, 339, 429, 435, 383, 417, 414, 401, 437, 443, 423, 427, 444, 438, 435, 438, 439, 440, 443, 451, 451, 439, 424, 424, 403, 457, 450, 422, 460, 459, 450, 401, 448, 460, 456, 465, 468, 453, 458, 444, 472, 470, 458, 468, 465, 437, 453, 479, 482, 456, 484, 463, 479, 485, 477, 477, 478, 457, 476, 448, 470, 492, 490, 478, 494, 484, 493, 440, 500, 476, 503, 494, 498, 499, 490, 498, 509, 512, 509, 493, 472, 512, 517, 515, 503, 518, 485, 500, 499, 520, 522, 524, 517, 555, 555, 524, 520, 518, 522, 531, 535, 537, 531, 536, 529, 542, 542, 515, 541, 541, 545, 538, 543, 548, 545, 536, 535, 543, 549, 529, 550, 554, 545, 549, 550, 548, 546, 564, 563, 546, 559, 557, 567, 557, 586, 586, 564, 570, 537, 576, 563, 576, 577, 567, 559, 581, 538, 580, 581, 554, 572, 583, 585, 580, 583, 577, 591, 570, 591, 572, 589, 594, 602, 602, 585, 584, 598, 608, 608, 598, 603, 612, 612, 589, 609, 607, 616, 616, 611, 594, 603, 611, 617, 614, 609, 620, 614, 617, 618, 624, 620, 621, 624, 626, 626, 622, 627, 627, 622, 632, 629, 621, 638, 638, 639, 631, 639, 629, 641, 607, 632, 641, 641, 641, 631, 644, 647, 650, 651, 644, 653, 653, 650, 656, 657, 651, 659, 662, 660, 662, 663, 663, 660, 666, 657, 668, 668, 666, 20))
vertices[(length(vertices$SUB) + 1),] <- 0
g <- graph.data.frame(edges, directed = TRUE, vertices = vertices)
subcomponent(g, 47, mode = "in")
答案 0 :(得分:1)
这里的问题是节点 indices 与节点 names 不一致。
head(V(g)$name, 20)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15"
[16] "16" "18" "19" "20" "21"
编写subcomponent(g, 47, mode = "in")
时,将获得具有 index 47的节点的子组件。但这是具有 name 48的节点。
V(g)$name[47]
[1] "48"
您可以使用subcomponent(g, "47", mode = "in")