Search code examples
rnetwork-programmingdynamiclibrariestemporal

Dynamic Network In R


I am currently working on dynamic temporal network.

Header: Time Sender Receiver

      1    1       2
      1    1       3
      2    2       1
      2    2       1
      3    1       2
      3    1       2

The above is a sample of my dataset. There are 3 time periods (sessions) and the edgelists between nodes. I want to compute centrality measures by each time period. I am thinking about writing a script that compute centrality measures within the same period of the time. However I am just wondering whether there might be R libraries that can handle this problem.

Is there anyone who knows about?

Jinie

I tried to write the code for subsetting data based on Time as follows:

uniq <-unique(unlist(df$Time))

uniq
[1] 1 2 3

for (i in 1:length(uniq)){

  t[i]<-subset(df, Time==uniq[i])

  net[i] <-as.matrix(t[i])

  netT[i]<-net[i][,-3]  #removing time column

  #### getting edgelist

  netT[i][,1]=as.character(net[i][,1])

  netT[i][,2]=as.character(net[i][,2])

  g [i]=graph.edgelist(netT [i], directed=T)

  g[i] 
}

however, I've got a error message ( Error in t[i] <- subset(df, Time == uniq[i]) : object of type 'closure' is not subsettable) Do you know why? I am kind of new to R so it is hard to figure it out. I guess t[i] is the problem. I don't know how to assign t[i] as a data frame.


Solution

  • The networkDynamic R library is helpful for this sort of thing (disclaimer: I'm a package maintainer)

    library(networkDynamic)
    
    # a data frame with your input data
    raw<-data.frame(time=c(1,1,2,2,3,3),
                  sender=c(1,1,2,2,1,1),
                receiver=c(2,3,1,1,2,2))
    
    # add another time column to define a start and end time for each edge spell
    raw2<-cbind(raw$time,raw$time+1,raw$sender,raw$receiver)
    
    # create a networkDynamic object using this edge timing info
    nd<-networkDynamic(edge.spells=raw2)
    
    # load the sna library with static network measures
    library(sna)
    
    # apply degree measure to static networks extracted at default time points
    lapply(get.networks(nd),degree)
     [[1]]
     [1] 2 1 1
    
     [[2]]
     [1] 1 1 0
    
     [[3]]
     [1] 1 1 0