构建一个邻接矩阵并在r中使用igraph

时间:2017-10-10 04:54:24

标签: r igraph adjacency-matrix

我正在关注此Building Adjacency matrix from DF

我把这个df作为玩具示例:

lf3 = structure(list(session_id = c(1L, 1L, 1L, 2L, 3L, 5L, 5L, 6L, 
6L, 7L), userId = c(1, 1, 1, 2, 2, 4, 4, 5, 5, 5), datetime = 
structure(c(1457029336, 
1457029337, 1457029340, 1457029596, 1457313569, 1457030783, 1457030784, 
1457030918, 1457030920, 1457370365), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), referer = c(22, 2, 7, 5, 23, 20, 7, 24, 18, 
22), request = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 5)), .Names = c("session_id", 
"userId", "datetime", "referer", "request"), row.names = c(NA, 
10L), class = "data.frame")

我尝试了这段代码:

lf3 %>% 
mutate_at(vars(referer,request),funs(factor(., levels))) %>% 
group_by(referer, request) %>%
tally() %>%
spread(request, n, drop=FALSE, fill=0)

但它给出了一些错误

mutate_impl(.data,dots)中的错误:'匹配'需要矢量参数

所以我试过这个:

lf3 %>% 
mutate_at(vars(referer,request),funs(factor(.))) %>% 
group_by(referer, request) %>%
tally() %>%
spread(request, n, drop=FALSE, fill=0)

但是不能使用igraph库制作网络图,因为它给出了n * n + 1维度 添加了1个额外的列用于引用.so graph.adjacency()无效。如何删除该引用额外列

0 个答案:

没有答案