如何使用igraph提取特定节点的邻居?

时间:2016-12-10 13:01:55

标签: r csv igraph vertices

我有.csv文件的人类疾病边缘列表。

Person   Diseases   Age    gender  location    
    Person1 Asthma  25  Female  Location1   
    Person2 Pneumonia   35  Male    Location2   
    Person3 Typhoid 40  Male    Location3   
    Person4 TB  36  Male    Location4   
    Person5 Asthma  25  Female  Location5   
    Person6 Pneumonia   50  Male    Location21  
    Person7 Typhoid 24  Female  Location22  
    Person8 TB  30  Female  Location23  
    Person9 Pneumonia   40  Male    Location24  
    Person10    Pneumonia   30  Male    Location25  
    Person11    Pneumonia   45  Female  Location26  
    Person12    Pneumonia   25  Male    Location3   
    Person13    Pneumonia   45  Male    Location4   
    Person14    Pneumonia   30  Male    Location5   
    Person15    Pneumonia   65  Male    Location15  
    Person16    Pneumonia   40  Female  Location16  
    Person17    TB  55  Male    Location17  
    Person18    TB  21  Male    Location18  
    Person19    TB  35  Female  Location11  
    Person20    TB  32  Male    Location12  
    Person21    TB  42  Male    Location13  
    Person22    TB  55  Female  Location14  
    Person23    TB  41  Male    Location21  
    Person24    TB  22  Female  Location22  
    Person25    Typhoid 31  Female  Location23  
    Person26    Typhoid 25  Female  Location24  
    Person27    Typhoid 67  Male    Location25  
    Person28    Typhoid 20  Female  Location26  
    Person29    Typhoid 36  Male    Location19  
    Person30    Typhoid 65  Female  Location20  
    Person31    Asthma  33  Female  Location6   
    Person32    Asthma  21  Male    Location7   
    Person33    Asthma  20  Female  Location8   
    Person34    Asthma  55  Male    Location9   
    Person35    Asthma  40  Female  Location10  
    Person36    Asthma  47  Male    Location11  
    Person37    Asthma  35  Male    Location12  
    Person38    Typhoid 30  Male    Location13  
    Person39    Typhoid 39  Female  Location23  
    Person40    Typhoid 21  Male    Location24  
    Person41    Asthma  40  Female  Location25  
    Person42    Asthma  19  Male    Location26  
    Person43    Asthma  29  Male    Location1   
    Person44    TB  49  Female  Location2   
    Person45    TB  24  Female  Location3   
    Person46    TB  68  Female  Location4   
    Person47    Typhoid 38  Female  Location5   
    Person48    Typhoid 59  Female  Location6   
    Person49    Typhoid 64  Male    Location7   
    Person50    Typhoid 31  Female  Location8   
    Person51    Typhoid 64  Female  Location20  
    Person52    Typhoid 32  Female  Location21  
    Person53    Asthma  30  Male    Location22  
    Person54    Asthma  36  Female  Location23  
    Person55    Asthma  40  Female  Location24  
    Person56    TB  20  Female  Location25  
    Person57    TB  26  Female  Location26  
    Person58    TB  42  Female  Location1   
    Person59    Typhoid 24  Female  Location2   
    Person60    Typhoid 48  Female  Location3   
    Person61    Typhoid 62  Male    Location4   
    Person62    Typhoid 42  Female  Location5   
    Person63    Typhoid 27  Female  Location6   
    Person64    Typhoid 36  Female  Location7   
    Person65    Asthma  67  Male    Location8   
    Person66    Asthma  23  Male    Location9   
    Person67    TB  21  Male    Location10  
    Person68    TB  45  Female  Location11  
    Person1 Arthritis   25  Female  Location1   
    Person2 heart attack    35  Male    Location2   
    Person3 diabetes    40  Female  Location3   
    Person4 heart attack    36  Male    Location4   
    Person5 Arthritis   25  Male    Location5   
    Person6 heart attack    50  Female  Location21  
    Person7 heart attack    24  Male    Location22  
    Person8 diabetes    30  Female  Location23  
    Person9 heart attack    40  Male    Location24  
    Person10    Arthritis   30  Male    Location25  
    Person11    heart attack    45  Female  Location26  
    Person12    Arthritis   25  Male    Location3   
    Person13    heart attack    45  Male    Location4   
    Person14    diabetes    30  Female  Location5   
    Person15    heart attack    65  Female  Location15  
    Person16    Arthritis   40  Female  Location16  
    Person17    heart attack    55  Female  Location17  
    Person18    heart attack    21  Female  Location18  
    Person19    Arthritis   35  Male    Location11  
    Person20    heart attack    32  Female  Location12  
    Person21    heart attack    42  Male    Location13  
    Person22    diabetes    55  Female  Location14  
    Person23    Arthritis   41  Female  Location21  
    Person24    heart attack    22  Female  Location22  
    Person25    diabetes    31  Female  Location23  
    Person26    heart attack    25  Female  Location24  
    Person27    Arthritis   67  Female  Location25  
    Person28    heart attack    20  Male    Location26  
    Person29    heart attack    36  Female  Location19  
    Person30    Arthritis   65  Female  Location20  
    Person31    heart attack    25  Female  Location6   
    Person32    heart attack    24  Male    Location7 

我已经尝试过这行代码来绘制一个人类及其疾病的二分网络,我已经展示了人物和位置"属于此代码的属性

V(g)$location <-  d_el$location
V(g)$color=V(g)$location    
V(g)$Person<- d_el$Person

现在我想提取TB节点的所有邻居节点我已经使用了这行代码

V(g)$location[which(V(g)$Person %in% neighbors(g, "TB"))] 
 [1] 20 22 14 16 17 20  8 15 16 17 18 19 11 13 24 25 20 21 20 22 14

以上输出显示了什么?

1 个答案:

答案 0 :(得分:2)

这应该有用(你的位置是因素,所以它被显示为整数,将它们转换为字符):

V(g)$location <-  as.character(d_el$location)
V(g)$Person <- as.character(d_el$Person)
V(g)$location[which(V(g)$Person %in% names(neighbors(g, "TB")))] 
# [1] "Location4"  "Location23" "Location17" "Location18" "Location11" "Location12" "Location13" "Location14" "Location21" "Location22" "Location2"  "Location3"  "Location4"  "Location25"
# [15] "Location26" "Location1"  "Location10" "Location11" "Location4"