Newl
Newl

Reputation: 330

Add named vector to attribute

I'm working with igraph, and I would like to assign a named vector to an attribute of my graph's vertex as the following:

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

However I get a warning every time, saying:

Warning message: In vattrs[[name]][index] <- value : number of items to replace is not a multiple of replacement length

How could I assign that vector to the attribute?

Upvotes: 1

Views: 362

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48241

As I understand you wanted to assign test.vec as an attribute only to the first vertex. It doesn't look like it's allowed to set a vector as a vertex attribute, however. But, we may assign a list:

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

or

(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

Verifying:

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

Upvotes: 2

Related Questions