igraph为一个顶点分配一个向量作为属性

4

我正在尝试将一个向量分配为顶点的属性,但没有成功:

# assignment of a numeric value (everything is ok)
g<-set.vertex.attribute(g, 'checked', 2, 3)
V(g)$checked

.

# assignment of a vector (is not working)
g<-set.vertex.attribute(g, 'checked', 2, c(3, 1))
V(g)$checked

查看手册,http://igraph.sourceforge.net/doc/R/attributes.html,似乎不可能实现。有没有什么解决方法?

到目前为止,我能想到的唯一方法是:

将此信息存储在另一个结构中

  • 将向量转换为带分隔符的字符串,并将其存储为字符串
1个回答

5

这个很好用:

## replace c(3,1) by list(c(3,1))
g <- set.vertex.attribute(g, 'checked', 2, list(c(3, 1)))
V(g)[2]$checked
[1] 3 1

编辑 为什么这样能行? 当你使用:

    g<-set.vertex.attribute(g, 'checked', 2, c(3, 1))

您会收到以下警告信息:
number of items to replace is not a multiple of replacement length

您试图将 c(3,1) 放置到一个长度为1的变量中,但其自身长度为2。所以需要用类似但长度为1的内容替换 c(3,1)。例如:

 length(list(c(3,1)))
[1] 1
> length(data.frame(c(3,1)))
[1] 1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接