【发布时间】:2019-12-25 05:26:55
【问题描述】:
问题是当我尝试应用 dplyr::mutate 函数以获得网络图列表的一些中心性度量时,遍历它们。我正在使用 tidygraph。
如果我对列表中的一个网络进行子集化,则代码运行良好,我的问题是当我尝试对整个列表执行此操作时。因此,在迭代 tbl 图时,它可能与 MAP 或 LAPPLY 函数有关。
library (tidyverse)
library (tidygraph)
#My df has the following characteristics
>df
origin destination year amount
1 Colombia Brasil 2005 16
2 Colombia US 2005 50
3 Colombia Argentina 2005 0
4 Colombia France 2006 5
5 Colombia US 2006 6
6 Brasil US 2005 5
7 Brasil Argentina 2005 7
8 Brasil France 2006 8
9 Argentina France 2005 0
10 Argentina US 2005 10
11 Argentina France 2006 5
#This is how I get the network graph list.
Networklist <- df %>%
filter(amount>0) %>%
group_by(year) %>%
group_map(as_tbl_graph)
#Then I apply the functions and that's where I get the error message
Networklist %>%
map(mutate(degreein = centrality_degree(weights = NULL, mode = "in", loops=FALSE, normalized=FALSE))
#Error: this function should not be called directly
到目前为止,我已经尝试过许多不同的映射功能,例如 map、lmap、lapply、sapply、modify 等,都会收到此错误消息。
谢谢!
【问题讨论】:
-
你能报告
dput(df)的输出吗? -
或提供导致问题的 df 示例数据
-
试试
map(~ mutate(.x, degreein = centrality_degree(weights = NULL, mode = "in", loops=FALSE, normalized=FALSE)))你需要一个lambda来调用purrr:map中的函数 -
@agila 这是一个例子。对不起,我是新人。
-
@paqmo 它符合您的建议,在变异之前添加 ~ !非常感谢!
标签: r error-handling dplyr igraph tidygraph