简体   繁体   English

tidygraph和igraph - 从数据框架差异构建图形

[英]tidygraph and igraph - build graph from dataframe discrepancy

I can build a graph object in igraph from two dataframes without problems. 我可以从两个数据帧中在igraph中构建一个图形对象而没有任何问题。 When I try do the same in tidygraph I get errors. 当我尝试在tidygraph中做同样的事情时,我会遇到错误。 Let me demonstrate. 让我来证明一下。 First I load my source data (data from a message board): 首先我加载源数据(来自留言板的数据):

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"))) 

Next, I create a unique node list (people who post things on a message board): 接下来,我创建一个唯一的节点列表(在留言板上发布内容的人):

node <- df %>% distinct(author_id, vendor) %>% rename(id = author_id) %>% mutate(vendor = as.numeric(vendor))

Then, my edge list (people connected via a discussion thread (topic)): 然后,我的边缘列表(通过讨论线程(主题)连接的人):

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)

Using igraph I can create this graph object: 使用igraph我可以创建这个图形对象:

test_net <- graph_from_data_frame(d = edge, directed = F, vertices = node)
plot(test_net)

This looks good. 这看起来不错。 Now I try do same with tidygraph: 现在我尝试用tidygraph做同样的事情:

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

Yikes! 哎呀! However, when I import the igraph object into tidygraph: 但是,当我将igraph对象导入tidygraph时:

tidy_net <- as_tbl_graph(test_net)
plot(tidy_net)

All works! 一切都有效! What is going on? 到底是怎么回事? Please help. 请帮忙。

I think because your nodes id and edges to and from are numeric, it assumes that there should be nodes for every integer between min(node$id) (2) and max(node$id) (512). 我想是因为你的节点id和边tofrom是数字,它假定应该有之间的每个整数节点min(node$id) (2)和max(node$id) (512)。 You can get around that by coercing them to characters. 你可以通过强迫他们到角色来解决这个问题。 Also, your iterpc command doesn't work properly for me, so I converted it to a tidyr version of expanding your data. 此外,您的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM