0

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)
MoonS
  • 117
  • 7
  • Please make sure to share your data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) otherwise it's difficult to copy/paste to run and test the code. How are you creating this adjacency matrix? Are you using `as_adjacency_matrix`? are you setting the `attr=` parameter? – MrFlick Oct 14 '20 at 06:04
  • Thanks for your comment and sorry for not being clear! I have added two lines of code where I use as.matrix get.adjacency(g, type="both", attr="Value") and then write the csv. It may also be that the error is happening when I try to write the file. I am somewhat new to r so I am struggling a bit with creating a reproducible example, but will try to fix it somehow. – MoonS Oct 14 '20 at 07:04
  • Have now included a reproducible example! – MoonS Oct 14 '20 at 07:24
  • With your example `as.matrix(get.adjacency(g, type="both", attr="Value"))` seems to work just fine. It uses the values of "Value" in the matrix. Is that not what you want? – MrFlick Oct 15 '20 at 03:04
  • The data contain values 0,2,112942, etc. These are the ones I would like to have in the matrix. For me, the code prints the csv as binary, only including 1's (and in my full code it prints with 1's and 2's). – MoonS Oct 15 '20 at 06:14
  • Really? That’s what you get when you run the exact code you shared in the reproducible example? What version if igraph are you using? – MrFlick Oct 15 '20 at 06:15
  • Apparently 1.2.5. Can this be the problem? – MoonS Oct 15 '20 at 07:39

1 Answers1

0

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)
MoonS
  • 117
  • 7