0

I'm doing analysis on company networks in R and am trying to export my igraph results into a dataframe.

Here's a reproducible example:

library(igraph)
sample <- data.frame(ID = 1:8, org_ID = c(5,4,1,2,2,2,5,7), mon = c("199801", "199802","199802","199802","199904","199912","200001", "200012"))

create.graphs <- function(df){
g <- graph.data.frame(d = df, directed = TRUE)
g <- simplify(g, remove.multiple = FALSE, remove.loops = TRUE)
E(g)$weight <- count_multiple(g)

#calculate global values
g$centrality <- centralization.degree(g)
#calculate local values
g$indegree <- degree(g, mode = "in",
                   loops = FALSE, normalized = FALSE)

return(g)
}

df.list <- split(sample, sample$mon)
g <- lapply(df.list, create.graphs)

As you can see, I have graphs for multiple months. I want to export this to longitudinal data, where each row represents a month (per ID) and each column represents the corresponding network measures.

So far I've managed to create a data frame, but not how to run it through the list of graphs and put it into a fitting format. An additional problem could be that the graphs have different numbers of nodes (some have around 25, others more than 40), but that should theoretically just be recognised as missing by my regression model.

output <- data.frame(Centrality = g$`199801`$centrality,
        Indegree = g$`199801`$indegree)
output
summary(output)

I tried writing a function similar to the one above for this, but unfortunately to no avail.

Thanks in advance for reading this, any help is greatly appreciated

SteffenT
  • 119
  • 6
  • You could try using `sapply` or `lapply` to mangle into a list form: `sapply(g, function(x){x$centrality})` and `lapply(g, function(x){x$indegree})`. This may be closer to the desire form. – Dave2e Jul 15 '17 at 03:44
  • Thank you, Dave2e and apologies for the late reply, I'm having a busy weekend :-) The `apply` functions seem like they are the right approach. One quick question, because I am still learning: `data.frame <- lapply(g, function(x){x$indegree})` would read: Apply every element of list g to data.frame where the function specifies what part ($) of each element (in this case indegree)? I find it difficult to wrap my head around some bits of the coding and want to develop a better understanding. – SteffenT Jul 16 '17 at 11:42

1 Answers1

0

I wanted to share how I solved it (thanks to Dave2e's suggestion).

Note that ci$monat defines my time periods in the original data, so one row for each point in time.

sumarTable <- data.frame(time = unique(ci$monat))

sumarTable$indegree <- lapply(g, function(x){x$indegree})
sumarTable$outdegree <- lapply(g, function(x){x$outdegree})
sumarTable$constraint <- lapply(g, function(x){x$constraint})

etc

edit: in order to export these values, I had to "flatten" the lists:

sumarTable$indegree <- vapply(sumarTable$indegree, paste, collapse = ", ", character(1L))
sumarTable$outdegree <- vapply(sumarTable$outdegree, paste, collapse = ", ", character(1L))
sumarTable$constraint <- vapply(sumarTable$constraint, paste, collapse = ", ", character(1L))
SteffenT
  • 119
  • 6
  • Hi Parfait, you are probably correct, thank you for pointing this out. I got to admit that I'm both new to R as well as to longitudinal data analysis, so I'm not 100% sure how to achieve the proper structure - very confusing so far, but rewarding when it works. Right now I have one row for each time point and, in the columns, lists according to `str`. They read `c(0,1,4,8,1,...)` though, so shouldn't they act as vectors? Or am I headed in the wrong direction? (edit: I should probably mention that I'd like to work with the data in Stata afterwards) – SteffenT Jul 16 '17 at 20:46
  • I'm not familiar with *igraph* but it sounds like it is ok. Yes, each column in a dataframe should be atomic vectors. if you can `write.csv()` without issue, there is no nested data. Happy coding! – Parfait Jul 16 '17 at 20:50
  • Thanks for the reply! I do get this error: `Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, : unimplemented type 'list' in 'EncodeElement'` when using `write.csv()`, so I guess I should somehow try to expand the vectors, because R treats them as lists? This would make the data essentially long format, I assume? – SteffenT Jul 17 '17 at 09:02