1
votes

I am trying to do a network analysis and after some qualitative data about culture I want to see how my culture nodes are related to each other. To do this I created a csv file in Excel with 46 rows and columns, aka it is a squared matrix. I tried the following two sets of code to get a network output in R.

First attempt (did not work):

library(igraph)
my_data <- read.csv(file.choose(),header=TRUE, row.names=NULL)
my_matrix <- as.matrix(my_data)
my_first_network <- 
graph.adjacency(my_matrix,mode="undirected",diag=FALSE)
my_first_network
plot(my_first_network)

I got these errors:

Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted, :
At structure_generators.c:274 : Non-square matrix, Non-square matrix
In addition:
Warning message:
In mde(x) : NAs introduced by coercion

Second attempt (also didn't work):

my_data <- read.csv(file.choose(),sep=",",header=TRUE)
nodelist <- names(my_data)[-1]
my_matrix <- as.matrix(my_data) [,-1]
rownames(my_matrix) <- colnames(my_matrix) <- nodelist
my_matrix
library(igraph)
g <- graph_from_adjacency_matrix(my_matrix, mode="undirected", 
weighted=NULL)
plot(g)

Got these errors:

Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted, :
At structure_generators.c:274 : Non-square matrix, Non-square matrix

Could anybody help me by telling me what the problem is, how I can fix the code to get my network?

Thank you very much in advance

2
what do you get from dim(my_matrix) ? - G5W
What do you mean @G5W ? Should I try and replace part of my code by that? Thank you :) - Lisanne Winter
No, Just run that code (after you have created my_matrix) and tell us the output. - G5W
[1] 61 1 # this is what I get right after I run dim(my_matrix) - Lisanne Winter
I edited my CSV document a little bit, I get this now when I run dim(my_matrix): [1] 45 0 - Lisanne Winter

2 Answers

0
votes

So like the error indicates: Non-square matrix, Non-square matrix the matrix isn't a square. The dim(my_matrix) [1] 45 0 tells the matrix is made up something like this:

 [1,]
 [2,]
 [3,]
 [4,]
 [5,]
 [..]

Could you show us the first rows of your .csv file? This can be done with head(my_data)

0
votes

For those finding this later; while the user's problem was a CSV error, graph adjacency requires a square matrix. So if you run dim(mat) and get something like 12 10 then you have 12 rows and 10 columns.

In a network analysis context this may mean not all or your origins are ever a destination (and vice versa, you have destinations that are never origins).