首页 文章

igraph将矢量指定为顶点的属性

提问于
浏览
3

我试图将矢量指定为顶点的属性,但没有任何运气:

# 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 回答

  • 4

    这很好用:

    ## 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
    

    EDIT 为什么会这样?当你使用:

    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)中的 length = 2置于长度= 1的变量中 . 所以想法是用类似的东西替换c(3,1),但长度= 1 . 例如:

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

相关问题