Build a square adjacency matrix from data.frame or data.table

我的梦境 提交于 2019-12-01 04:02:31
A5C1D2H2I1M1N2O1R2T1

It sounds like you're just looking for table, but you should make sure that both columns have the same factor levels:

levs <- unique(unlist(dt, use.names = FALSE))
table(lapply(dt, factor, levs))
#       Target
# Source a b c d
#      a 0 0 0 2
#      b 0 0 2 0
#      c 0 2 0 0
#      d 0 0 0 0

I don't know if it would offer any speed improvements, but you could also use dcast from "data.table":

dcast(lapply(dt, factor, levs), Source ~ Target, drop = FALSE,
      value.var = "Target", fun.aggregate = length)

You can also use igraph. Since you said that you are dealing with large data, igraph has the advantage that it uses sparse matrices:

library(igraph)
g <- graph_from_data_frame(dt)
as_adjacency_matrix(g)
4 x 4 sparse Matrix of class "dgCMatrix"
  a b c d
a . . . 2
b . . 2 .
c . 2 . .
d . . . .

We can use dplyr/tidyr

library(dplyr)
library(tidyr)
dt %>% 
   mutate_each(funs(factor(., letters[1:4]))) %>% 
   group_by(Source, Target) %>%
   tally() %>%
   spread(Target, n, drop=FALSE, fill=0)
#  Source     a     b     c     d
#   (fctr) (dbl) (dbl) (dbl) (dbl)
#1      a     0     0     0     2
#2      b     0     0     2     0
#3      c     0     2     0     0
#4      d     0     0     0     0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!