0

i'm new to coding and I'm trying to perform a network analysis in R. i've got a data frame with 2 columns, one country of origin and the other is destination. I am trying to calculate betweenness and closeness centrality in R. But when trying to graph using :

g <- graph (c2, directed=TRUE)

, where c2 is the name of my DF as described above, I get the message:

Error in graph(c2, directed = TRUE) : 
  'edges' must be numeric or character. 

I've already tried converting factors to characters by the following:

c2 <- data.frame(lapply(c2, as.character), stringsAsFactors=F)

but for some reason it is not helping. What have I done wrong?

edit* - dput(head(c2)) gives:

> dput(head(c2))
structure(list(CountryID.Origin = c("india", "malaysia", "fiji", 
"fiji", "china", "united states (the)"), State = c("ACT", "ACT", 
"ACT", "ACT", "NSW", "NSW")), row.names = c(NA, 6L), class = "data.frame")
Abilash
  • 1
  • 4
  • 3
    (1) Please show us the data in an unambiguous format, such as `dput(head(c2))`; copying the columnar-*representation* of a frame is often insufficient, especially in cases like this where its mode/type/class comes into question. (2) Please check any/all code you put into the question: you have one-too-many parens in your first code block, for instance, is there more to the command you missed pasting into the question? – r2evans Jun 30 '19 at 05:30
  • Thanks for the reply. if copying the columnar-representation is insufficient, how should I go about it in this case? How can I code so that R recognizes one column to be source, and the other to be destination country, so that I can graph a directed network? The parentheses have been edited. – Abilash Jun 30 '19 at 05:40
  • *"copying the columnar-representation is insufficient"* for us to help, not for your code. Please edit your question with the result of `dput(head(c2))` – PavoDive Jun 30 '19 at 06:03

3 Answers3

1

As pointed out by @r2evans and @PavoDive it is important that you provide some data along with the question you are posing. Notwithstanding, here is something you could try. I have made some assumption about what your data might look like:

library(igraph)

df <- data.frame(Origin = c('India', 'China', 'UK', 'Russia', 'USA', 'Brazil', 'Germany', 'India', 'UK', 'China'),
                 Destination = c('China', 'UK', 'India', 'UK', 'India', 'UK', 'Russia', 'China', 'India', 'India'))

#Convert to a matrix
df.mat <- as.matrix(df)

#Convert to an igraph object
g <- graph.edgelist(df.mat, directed = TRUE)

# Make a very basic plot of the network
plot(g)

betweenness(g)
Dhiraj
  • 1,650
  • 1
  • 18
  • 44
0

You cannot pass a data frame to graph. It expects a vector like this

("china","india","USA","india","italy","china") 

where the edges are the first pair of elements, the second pair of elements and so on. so in this example you have an edge china—india, an edge USA—india, and an edge italy—china.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
FGirosi
  • 106
  • 5
0

You are looking for the function graph_from_data_frame from igraph:

library(igraph)
library(countrycode)
library(tidyverse)

df_country = countrycode::codelist %>% as_tibble()

# data frame with edge list
df_network = tibble(
  origin = sample(df_country$country.name.en, size = 1000, replace = TRUE),
  destination = sample(df_country$country.name.en, size = 1000, replace = TRUE)
)

g_network = graph_from_data_frame(df_network, directed = TRUE)
betweenness(g_network)
plot(g_network)