0

I have a Dataset which looks like this:

PostID UserID ReplyTo
1      11     NA
2      12     1
3      11     2
4      13     NA
5      12     4

I want to have a directed Graph with the Edges: 12->11, 11->12, 12->13

The Idea is that ad the End i'm capable of having a list of UserID's to which i add the Different Centralities.

The way i tried it i was only capable of producing a graph between the PostID's and not the UserID's.

library(igraph)

D <- data.frame(Data$PostID, Data$ReplyTo)
FinalEdges<-D[complete.cases(D),]
FinalNetwork <- graph.data.frame(FinalEdges, directed = TRUE)


plot(FinalNetwork, vertex.label = V(FinalNetwork)$PostID) 

For the Different Centralities i use:

DegreeCentrality<-degree(FinalNetwork)
ClosenessCentrality<-closeness(FinalNetwork)
BetweennessCentrality<-betweenness(FinalNetwork)
which i the would like to add to a list of users.

At The End i would like to have A List of Users and their Centralities

UserID, DegreeCentrality, Closeness Centrality, BetweennessCentrality
11
12
13
Carlo
  • 397
  • 1
  • 3
  • 14
  • One way to get the graph as you want is to define a `to` column to be used in `graph.data.frame`. So `dat$to <- dat$UserID[match(dat$ReplyTo, dat$PostID)]` and then `FinalNetwork <- graph.data.frame(na.omit(dat[c("UserID", "to")]), directed = TRUE)` – user20650 Aug 14 '14 at 15:53

1 Answers1

1

You'll need to reshape your input data to connect the pairs. If this is your sample input data

dd<-structure(list(PostID = 1:5, UserID = c(11L, 12L, 11L, 13L, 12L
), ReplyTo = c(NA, 1L, 2L, NA, 4L)), .Names = c("PostID", "UserID", 
"ReplyTo"), class = "data.frame", row.names = c(NA, -5L))

Then you can merge the replies with

 mm<-merge(dd, dd, by.x="ReplyTo", by.y="PostID")

This produces

  ReplyTo PostID UserID.x UserID.y ReplyTo
1       1      2       12       11      NA
2       2      3       11       12       1
3       4      5       12       13      NA

as well as a warning that the ReplyIo column is repeated. But that's not really a problem. We just need the two UserID columns. We can turn those into a graph

library(igraph)
gg <- graph.data.frame(mm[,c("UserID.x","UserID.y")])

enter image description here

With that object you should be able to use the igraph library to calculate whatever statistics you want.

MrFlick
  • 195,160
  • 17
  • 277
  • 295