首页 > 解决方案 > 将顶点属性添加到 igraph 对象时出错:seq_len(no) 中的错误:参数必须强制转换为非负整数

问题描述

我有纵向网络数据(3 个时间点),其中并非所有节点都存在于每个时期。我还为三个边缘列表中的每一个创建了属性列表,以确保我在各自的属性数据中只有每个边缘列表中的节点。由于我想通过 ID 匹配 edgelist 和节点属性,我确保属性数据中的第一列是 ID,并且 edgelist 设置为 ego_id、alter_id、weight。

这是我的边缘列表示例:

edgelist_time1 <- tibble::tribble(
              ~ego_id, ~alter_id, ~weight,
                   7L,        8L,      7L,
                   7L,        9L,      7L,
                   7L,      113L,      1L,
                   7L,      114L,      1L,
                   7L,      115L,      1L,
                   8L,        7L,      7L,
                   8L,        9L,      4L,
                   8L,       11L,      6L,
                   8L,       12L,      6L,
                   8L,       13L,      6L,
                   8L,       15L,      4L,
                   9L,        7L,      4L
              )
head(edgelist_time1)
#> # A tibble: 6 x 3
#>   ego_id alter_id weight
#>    <int>    <int>  <int>
#> 1      7        8        7
#> 2      7        9        7
#> 3      7      113      1
#> 4      7      114      1
#> 5      7      115      1
#> 6      8        7         7

我从一个有效的边缘列表创建了一个 igraph 对象。

contact_time1_net <- graph_from_data_frame(d=edgelist_time1, directed = T)

contact_time1_net

输出:

IGRAPH a75a5df DNW- 137 360 -- 
+ attr: name (v/c), weight (e/n)
+ edges from a75a5df (vertex names):
  [1] 7 ->8   7 ->9   7 ->113 7 ->114 7 ->115 8 ->7   8 ->9   8 ->11  8 ->12  8 ->13  8 ->15  9 ->7   9 ->8  
 [14] 9 ->10  9 ->11  9 ->12  9 ->13  9 ->14  9 ->15  10->11  10->12  10->113 10->116 10->117 11->7   11->9  
 [27] 11->10  11->12  11->13  11->14  11->15  12->10  12->11  12->13  12->14  12->124 13->14  13->15  13->117
 [40] 13->125 13->191 14->7   14->9   14->11  14->12  14->13  14->15  16->10  16->11  16->12  16->13  16->14 
 [53] 17->19  17->20  17->101 17->122 17->127 18->16  18->17  18->20  18->113 18->118 19->16  19->17  19->102
 [66] 19->117 19->121 19->122 19->123 20->16  20->17  20->18  20->19  20->118 21->17  21->22  21->44  21->119
 [79] 21->120 22->17  22->21  22->120 22->128 22->129 22->130 24->25  24->26  24->27  24->28  24->29  24->30 
 [92] 24->31  24->46  24->132 25->24  25->27  25->28  25->29  25->31  25->82  25->138 25->147 26->24  26->25 
+ ... omitted several edges

但是,每当我尝试分配我的顶点/节点属性时,它都会给我一个错误:seq_len(no) 中的错误:参数必须强制转换为非负整数

contact_time1_net <- graph_from_data_frame(d=edgelist_time1, directed = T, vertices=attributes_time1)

contact_time1_net

这是我的属性数据的示例:

attributes_time1 <- tibble::tribble(
                      ~ID, ~plant_t1,    ~name_t1, ~level_t1, ~participation_t1, ~response_time_min_t1,
                       7L,        1L,    "Daniel",        4L,                1L,                   35L,
                       8L,        1L,      "Luca",        4L,                1L,                   21L,
                       9L,        1L,     "Dejan",        4L,                1L,                   36L,
                      10L,        1L,   "Melissa",        4L,                1L,                   30L,
                      11L,        1L,    "Markus",        4L,                1L,                   14L,
                      12L,        1L,      "Sven",        4L,                1L,                   36L,
                      13L,        1L, "christian",        4L,                1L,                   23L,
                      14L,        1L,     "Milan",        4L,                1L,                   21L
                      )
head(attributes_time1)
#> # A tibble: 6 x 6
#>      ID plant_t1 name_t1 level_t1 participation_t1 response_time_min_t1
#>   <int>    <int> <chr>      <int>            <int>                <int>
#> 1     7        1 Daniel         4                1                   35
#> 2     8        1 Luca           4                1                   21
#> 3     9        1 Dejan          4                1                   36
#> 4    10        1 Melissa        4                1                   30
#> 5    11        1 Markus         4                1                   14
#> 6    12        1 Sven           4                1                   36

输出:

IGRAPH 15f9f29 DNW- 187 360 -- + attr: name (v/c), plant_t1 (v/n), name_t1 (v/c), last_name_t1 (v/c), level_t1 (v/n), | participation_t1 (v/n), response_time_min_t1 (v/n), interest_tech_t1 (v/n), technological_skill_t1 | (v/n), training_t1 (v/n), training_app_t1 (v/n), awareness_t1 (v/n), availability_t1 (v/n), | availability_app_year_t1 (v/n), startusage_app_year_t1 (v/n), future_usage_t1 (v/n), | future_usage_frequency_t1 (v/n), current_usage_frequency_t1 (v/n), usage_machinestatus_t1 (v/n), | usage_measurementreport_t1 (v/n), usage_measuredvalues_t1 (v/n), push_machinestatus_t1 (v/n), | push_measurementreport_t1 (v/n), push_measuredvalues_t1 (v/n), ease_usage_t1 (v/n), | seed_usage_1_t1 (v/n), seed_usage_2_t1 (v/n), seed_usage_3_t1 (v/n), scaling_t1 (v/n), | sentimement_t1 (v/n), management_importance_t1 (v/n), gender_t1 (v/n), age_t1 (v/n), educ_t1 | (v/n), occup_t1 (v/n), management_2sition_t1 (v/n), language_1_t1 (v/n), language_2_t1 (v/n), | language_3_t1 (v/n), device_system_1_t1 (v/n), Covid_control_t1 (v/n), covid_changes_t1 (v/n), | covid_changes_2_t1 (v/n), weight (e/n) + edges from 15f9f29 (vertex names):

Traceback:
error in seq_len(no) : argument must be coercible to non-negative integer

17. eval(x$expr, data, x$env)

16. eval(x$expr, data, x$env)

15. FUN(X[[i]], ...)

14. lapply(x, lazy_eval, data = data)

13. lazy_eval(args, data = c(attrs, .inc = .inc, inc = inc, adj = adj, .from = .from, from = from, .to = .to, to = to, .igraph.from = list(.Call(C_R_igraph_mybracket, graph, 3L)[as.numeric(x)]), .igraph.to = list(.Call(C_R_igraph_mybracket, graph, 4L)[as.numeric(x)]), .igraph.graph = list(graph), ...

12. `[.igraph.es`(edges, seq_len(no))

11. edges[seq_len(no)]

10. na.omit(es)

9. as.igraph.es(graph, na.omit(es))

8. ends(x, edges[seq_len(no)], names = names)

7. x("width", no = can_max)

6. head_print_callback(x, max_lines, header, footer, omitted_footer, ...)

5. head_print(fun, max_lines = max.lines)

4. .print.edges.compressed.limit(x, edges, names, max.lines)

3. .print.edges.compressed(x, names = names, max.lines = max.lines - head_lines)

2. print.igraph(x)

1. (function (x, ...) UseMethod("print"))(x)

也许,任何人都知道问题可能是什么。

提前谢谢了!

标签: rigraph

解决方案


你应该知道你的attributes_time1并没有覆盖所有的顶点。在这种情况下,您需要使用merge.


你可以试试这段代码

v_attr <- merge(data.frame(ID = unlist(edgelist_time1[1:2])), attributes_time1, all = TRUE)
contact_time1_net <- graph_from_data_frame(d = edgelist_time1, directed = T, vertices = unique(v_attr))

你会看到

> vertex.attributes(contact_time1_net)
$name
 [1] "7"   "8"   "9"   "10"  "11"  "12"  "13"  "14"  "15"  "113" "114" "115"

$plant_t1
 [1]  1  1  1  1  1  1  1  1 NA NA NA NA

$name_t1
 [1] "Daniel"    "Luca"      "Dejan"     "Melissa"   "Markus"    "Sven"
 [7] "christian" "Milan"     NA          NA          NA          NA

$level_t1
 [1]  4  4  4  4  4  4  4  4 NA NA NA NA

$participation_t1
 [1]  1  1  1  1  1  1  1  1 NA NA NA NA

$response_time_min_t1
 [1] 35 21 36 30 14 36 23 21 NA NA NA NA

推荐阅读