我可以从两个数据文件中构建一个图形对象,没有问题。当我尝试做同样的计时器时,我会出错。让我演示一下。首先,我加载我的源数据(来自留言板的数据):
library(dplyr)
library(tidyr)
library(tidygraph)
library(lubridate)
library(iterpc)
library(igraph)
df <- data.frame(author_id = c(2,4,8,16,4,8,2,256,512,8),
topic_id = c(101,101,101,101,301,301,501,501,501,501),
time = as.POSIXct(c("2011-08-16 20:20:11", "2011-08-16 21:10:00", "2011-08-17 06:30:10",
"2011-08-17 10:08:32", "2011-08-20 22:23:01","2011-08-20 23:03:03",
"2011-08-25 17:05:01", "2011-08-25 19:15:10", "2011-08-25 20:07:11",
"2011-08-25 23:59:59")),
vendor = as.logical(c("FALSE", "FALSE", "TRUE", "FALSE", "FALSE",
"TRUE", "FALSE", "FALSE", "FALSE", "TRUE"))) 接下来,我创建了一个唯一的节点列表(在留言板上发布信息的人):
node <- df %>% distinct(author_id, vendor) %>% rename(id = author_id) %>% mutate(vendor = as.numeric(vendor))然后,我的边缘列表(通过讨论线程(主题)连接的人):
edge <- df %>%
group_by(topic_id) %>%
do(data.frame(getall(iterpc(table(.$author_id), 2, replace =TRUE)))) %>%
filter(X1 != X2) %>% rename(from = X1, to = X2) %>% select(to, from, topic_id)使用igraph可以创建这个图形对象:
test_net <- graph_from_data_frame(d = edge, directed = F, vertices = node)
plot(test_net)这个看起来不错。现在,我试着做同样的计时器:
tidy_net <- tbl_graph(nodes = node, edges = edge, directed = F)
Error in add_vertices(gr, nrow(nodes) - gorder(gr)) : At type_indexededgelist.c:369 : cannot add negative number of vertices, Invalid value呀!但是,当我将i图形对象导入TidyGraph时:
tidy_net <- as_tbl_graph(test_net)
plot(tidy_net)一切都成功了!怎么一回事?请帮帮忙。
发布于 2018-05-22 15:28:17
我认为,因为您的节点id、边to和from都是数字的,所以假定min(node$id) (2)和max(node$id) (512)之间的每个整数都应该有节点。你可以通过强迫他们扮演角色来避开这个问题。而且,您的iterpc命令对我来说不能正常工作,所以我将它转换为扩展数据的tidyr版本。
node <-
df %>%
distinct(author_id, vendor) %>%
rename(id = author_id) %>%
mutate(vendor = as.numeric(vendor)) %>%
mutate(id = as.character(id))
edge <-
df %>%
group_by(topic_id) %>%
expand(topic_id, from = author_id, to = author_id) %>%
filter(from < to) %>%
select(to, from, topic_id) %>%
mutate_at(vars(to, from), as.character)
tidy_net <- tbl_graph(nodes = node, edges = edge, directed = F)
plot(tidy_net)https://stackoverflow.com/questions/50457926
复制相似问题