Merge or combine vertex in igraph

Viewed 31

I have a list of repeated nodes (vertex) in an igraph file that I would like to unify while keeping the connections. In my example I have an author listed as:

R, 2000-1
R, 2000-2
J, 1999-1
J, 1999-2
J, 1999-3

And it would have to be a single author like

R, 2000
J, 1999

I've been looking for a solution all day but I haven't been able to do it in a simple way. I have also tried to go from igraph to csv and back to igraph again, but this option gives me problems (I lose some connections between edges).

1 Answers

I don't think you need igraph. You can try the following base R code

unique(
  transform(
    df,
    V2 = gsub("-\\d+", "", V2)
  )
)

which gives

  V1   V2
1  R 2000
3  J 1999

Data

> dput(df)
structure(list(V1 = c("R", "R", "J", "J", "J"), V2 = c("2000-1",
"2000-2", "1999-1", "1999-2", "1999-3")), class = "data.frame", row.names = c(NA,
-5L))
Related