我想networkDynamic
在 R 中从交易数据构造一个对象,其中每一行代表一个人对文档的贡献。多重贡献应该表示为边权重的增加,而不是创建多个边。
以下代码片段应该可以在 RStudio 中轻松重现以查看问题。
if (!require("pacman")) install.packages("pacman"); library("pacman")
pacman::p_load(network, networkDynamic, ndtv, lubridate)
stTransac <- "
'person', 'document', 'weight', 'instantId'
'A', 'a1', '3', '1'
'A', 'a1', '15', '2'
'A', 'a1', '100', '3'
'B', 'a1', '20', '10'
'C', 'a1', '30', '12'
"
dfTransac <- read.csv(text = stTransac, sep = "," , quote = '\'' , strip.white = TRUE, stringsAsFactors = FALSE)
net <- network.initialize(0, directed = TRUE, bipartite = 3)
add.vertices.networkDynamic(net, 3, vertex.pid = c("A","B","C"))
add.vertices.networkDynamic(net, 1, vertex.pid = "a1")
net %v% "vertex.names" <- c(c("A","B","C"), "a1")
set.network.attribute(net,'vertex.pid','vertex.names')
set.network.attribute(net,'edge.pid','edge.names')
add.edges.networkDynamic(net,
tail = get.vertex.id(net, c("A","B","C")),
head = get.vertex.id(net, "a1"),
edge.pid = paste0(c("A","B","C"), "->a1"))
activate.edges(net,
e = get.edge.id(net, paste0(dfTransac[["person"]], "->a1")),
at = dfTransac$instantId)
到目前为止,一切都按预期工作(如果您跳过activate.edge.attribute
下面的块并直接跳转到最后一个块,您将在动画中看到边缘在 1、2、3、10、12 时被激活。)但是,当使用该activate.edge.attribute
函数直观地以与函数相同的方式activate.edges
,对于第一条边,权重属性仅用3
的值初始化100
。前两个权重值被删除。
activate.edge.attribute(net,
prefix = "weight",
value = dfTransac$weight,
e = get.edge.id(net, paste0(dfTransac[["person"]], "->a1")),
at = dfTransac$instantId)
我可以遍历事务数据框,但我想这不会很好地扩展:
by(dfTransac, 1:nrow(dfTransac), function(row) {
net <<- activate.edge.attribute(net,
prefix = "weight",
value = row[["weight"]],
e = get.edge.id(net, paste0(row[["person"]], "->", row[["document"]])),
at = row[["instantId"]])
})
最后一个块渲染动画......
reconcile.vertex.activity(net = net, mode = "encompass.edges", edge.active.default = FALSE)
compute.animation(net, slice.par = list(start = 1, end = 13, interval = 1, aggregate.dur = 1, rule = "any"))
render.animation(net)
ani.replay()
at
设置多个不同时间戳的权重属性的正确有效方法是什么?