1

I have an igraph where the vertexes only have their IDs (V(g)$name). I also have a data.frame in this format that match each ID to a name:

ID   name
1    Ann
2    Bob
3    Carl
...

I would like to add the names to the vertex (V(g)$label) by matching the ID. The graph does not have all the IDs the names frame has.

I think it should be rather simple but as I'm new to R I'm not quite sure as how to do it.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
user3388408
  • 131
  • 8
  • 1
    Try using `?match` – akrun Mar 20 '16 at 16:25
  • Hi, welcome to SO. Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It makes it easier for others to help you. – Heroka Mar 20 '16 at 16:25

1 Answers1

1

As suggested in the comments, match could do the trick:

df <- read.table(header=T, text="
ID   name
1    Ann
2    Bob
3    Carl
4    Linda
5    Peter")
library(igraph)
g <- sample_pa(4)
V(g)$label <- as.character(df$name[match(V(g), df$ID)])
lukeA
  • 53,097
  • 5
  • 97
  • 100