【问题标题】:Add named vector to attribute将命名向量添加到属性
【发布时间】:2018-12-04 16:53:50
【问题描述】:

我正在使用 igraph,我想将一个命名向量分配给我的图形顶点的属性,如下所示:

library(igraph)

test.graph <- graph.famous('bull')
test.vec <- c(0,0,0)
names(test.vec) <- c('a','b','c')
V(test.graph)[1]$test.attr <- test.vec

但是我每次都会收到警告,说:

警告信息:在 vattrs[[name]][index]

如何将该向量分配给属性?

【问题讨论】:

  • 你有问题V(test.graph)[1]$test.attr &lt;- test.vec,矢量部分的命名工作正常。
  • 您能说得更具体些吗?警告只是说您正在尝试将长度为 1 的内容替换为长度为 3 的内容(test.vec)
  • V(test.graph)[1] 是一个长度为1 的对象,而test.vec 是一个长度为3 的对象。您需要使两个长度匹配。例如。 - test.graph$test.attr &lt;- 1:5 工作得很好。
  • @paoloeusebi 我想将命名向量或列表分配给顶点属性,因此当我进行进一步计算/工作时,我可以使用字符串从该顶点属性列表中选择一个项目。

标签: r igraph


【解决方案1】:

据我了解,您希望将test.vec 作为属性仅分配给第一个顶点。但是,看起来不允许将向量设置为顶点属性。但是,我们可以指定一个列表:

V(test.graph)[1]$test.attr <- list(test.vec)

(test.graph <- set.vertex.attribute(test.graph, "test.attr", 
                                    index = 1, list(test.vec)))
# IGRAPH ade745b U--- 5 5 -- Bull
# + attr: name (g/c), test.attr (v/x)
# + edges from ade745b:
# [1] 1--2 1--3 2--3 2--4 3--5

验证:

get.vertex.attribute(z, "test.attr")
# [[1]]
# a b c 
# 0 0 0 
#
# [[2]]
# NULL
#
# [[3]]
# NULL
#
# [[4]]
# NULL
#
# [[5]]
# NULL

【讨论】:

  • 嗨 Julius 我在 igraph 中对项目进行排序时遇到问题。几天前我发布了一个问题,但没有得到答案。您能否看一下:如何在 R 中对 igraph 图中的重复值进行排序? 谢谢。
猜你喜欢
  • 2013-09-21
  • 2012-02-20
  • 1970-01-01
  • 2020-11-05
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多