1

This post is related to a previous question.

The basic problem in that post was how to connect nodes by common retweeted users. The code suggested by @ThomasIsCoding does work.

My follow up question is how to make this function store edge attributes. I provide a toy example below:

My initial dataframe is of the form:

author.id<-c("A","B","D")
rt_user<-c("C","C","C")
id<-c(1,2,3)

example<-data.frame(author.id,rt_user,id)

Where nodes A,B,D retweet from node C and the id column is a numeric classifier for the tweets. Using this in a network structure and applying the aforementioned function leads:

g <- graph.data.frame(example, directed = T)
edge.attributes(g)
   gres <- do.call(
  graph.union,
  lapply(
    names(V(g))[degree(g, mode = "out") == 0],
    function(x) {
      nbs <- names(V(g))[distances(g, v = x, mode = "in") == 1]
      disjoint_union(
        set_vertex_attr(make_full_graph(length(nbs)), name = "name", value = nbs),
        set_vertex_attr(make_full_graph(1), name = "name", value = x)
      )
    }
  )
)

plot(gres)
edge.attributes(gres)

My goal is to "preserve" the edge attributes of g in the final transformation of gres. I want to know that A used tweet 1, B used tweet 2 and D tweet 3. I believe now they should be transformed from edge to vertex attributes so that one still knows who is the user and which is the tweet but I am not sure on how to incorporate this into the code.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
Luis
  • 330
  • 1
  • 11

1 Answers1

1

You can try the code below

gres <- set_vertex_attr(gres,
    name = "id",
    value = E(g)$id[match(names(V(gres)), names(tail_of(g, E(g))))]
)

where the edge attributes are characterized vertices from tail_of, and you will see

> V(gres)$id
[1]  1  2  3 NA
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81