Converting edgelist to graph doesn't keep values

84 views Asked by At

I trying to convert an an edgelist to a weighted graph (i.e. keeping the values in the edgelist). Here is my edgelist:

Reporter.Countries Partner.Countries                 Year   Value
 1 Afghanistan        Canada                           2017       0 
 2 Afghanistan        Pakistan                         2015       2 
 3 Afghanistan        Slovakia                         2018       0 
 4 Albania            Iceland                          2017       0 
 5 Algeria            Senegal                          2017       0 
 6 Argentina          Bangladesh                       2014. 112942 
 7 Argentina          Belgium                          2016.      0 
 8 Argentina          Bolivia (Plurinational State of) 2016    7556 
 9 Argentina          Brazil                           2016     411.
10 Argentina          Canada                           2016.    364.

This is the code I am using:

#import data and average years
soybean <- read.csv("soybean.csv")

library(dplyr)
soybean <- soybean %>%
  group_by(Reporter.Countries, Partner.Countries) %>% 
  summarise_each(funs(mean))

#make edgelist
edgelist <- soybean %>%
  select(Reporter.Countries, Partner.Countries, Year, Value)

edgelist$"Value" <- as.numeric(edgelist$"Value")

#converting to graph
g <- graph.data.frame(edgelist, directed=FALSE)

g <- set_edge_attr(g, "Value", value= edgelist$Value)

get.adjacency(g, type="both", attr="Value")

write.csv(as.matrix(get.adjacency(g)), file = "importedsoybean.csv", row.names = TRUE)

But my adjacency matrix ends up with 1's and 2's instead of the values in the edgelist.

It looks like this:

Afghanistan                      . . . . . . . . . . . . . . . . . . . . . . . . . 2 .
Albania                          . . . . . . . . . . . . . . . . . . . . . . . . . . .
Algeria                          . . . . . . . . . . 1 . . . . . 1 . . . . . . . . . .
Argentina                        . . . . . . . . 1 . 1 . . 2 . . 2 . . . . . .

What am I doing wrong?

Here is a reproducible example:

df <- read.table(header=TRUE, 
text="Reporter.Countries Partner.Countries Year Value                                                                                                                                                                                                                                                                                                                                                               
1          Afghanistan         Canada          2017         0  
2          Afghanistan         Pakistan          2015         2  
3          Afghanistan         Slovakia          2018         0  
4          Albania        Iceland          2017         0  
5          Algeria         Senegal          2017         0  
6          Argentina             Bangladesh          2014         112942  
")                                                                                                                                                                                                                                                                                                                                               

g <- graph.data.frame(df, directed=FALSE)

g <- set_edge_attr(g, "Value", value= edgelist$Value)

get.adjacency(g, type="both", attr="Value")

get.adjacency(g)

write.csv(as.matrix(get.adjacency(g)), file = "importedsoybeantext.csv", row.names = TRUE)
1

There are 1 answers

0
MoonS On BEST ANSWER

I managed to solve this problem by:

f <- as.matrix(get.adjacency(g, type="both", attr="Value"))

write.csv(f, file = "importedsoybean.csv", row.names = TRUE)