Search code examples
rcsvionamesstatnet

Assigning arbitrary names to variables in R


After reading in a CSV of attributes, I'd like to apply these to an existing object (using a statnet-specific convention). If I knew the names ahead of time, I would do this:

pred_net %v% "id" <- nodeInfo$id
pred_net %v% "age" <- nodeInfo$age
pred_net %v% "sex" <- nodeInfo$sex
pred_net %v% "handed" <- nodeInfo$handed
pred_net %v% "lastDocVisit" <- nodeInfo$lastDocVisit

This works just fine, however, I don't know the names (id, age, sex, etc.) and would like to do something like this:

for (n in names(nodeInfo)) {
    pred_net %v% n <- nodeInfo$n
}

...which gives me an error:

Error in set.vertex.attribute(x, attrname = attrname, value = value) : 
  Inappropriate value given in set.vertex.attribute.

Presumably, this is because the variable names n are not handled as strings to pass to the %v% operator. Any ideas?


Solution

  • You can select a variable also with [[characterVariable]] (supposing nodeInfo it's a list or a data.frame) :

    for (n in names(nodeInfo)) {
        pred_net %v% n <- nodeInfo[[n]]
    }