-2

How to import an adjacency array from a file in .txt format with edge notation (v, w) in RStudio?

The contents of the .txt file are as follows:

5vertices,não dirigido
0,1
1,2
1,3
2,3
3,4
4,0
Reinforcement that this is the notation of vertices in the format (v, w).
vertices: 0 to 4
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
Tiago
  • 11
  • 1

1 Answers1

0

Your question is not very clear nor reproducible. For example, I don't know what you mean by an "adjacency array".

That aside, assuming you have a text file (which I call sample.txt here) with the following content

5vertices,não dirigido
0,1
1,2
1,3
2,3
3,4
4,0
Reinforcement that this is the notation of vertices in the format (v, w).
vertices: 0 to 4

you can use readLines to read the file line-by-line, extract the edge list and create an igraph object:

ln <- readLines("sample.txt")

# Store as matrix with from/to indices
vtx <- do.call(rbind, strsplit(ln[grep("\\d+,\\d+", ln)], ","))

# Convert indices to integer and convert to igraph
library(igraph)
ig <- graph_from_data_frame(apply(vtx, 2, as.integer))

plot(ig)

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • very good!! Thank you!! that's exactly what I was asking. Now, I could not identify how to make the graph not directed, how would it be? once, thank you – Tiago Apr 15 '19 at 13:05
  • Get! just add the "directed = False" argument in the "graph_form_data_frame" function: ig <- graph_from_data_frame (apply (vtx, directed = FALSE, 2, as.integer), directed = FALSE. – Tiago Apr 15 '19 at 13:35
  • @Tiago If this answers your question please close the question by setting the green checkmark next to the answer. That way you help keeping SO tidy and make it easier for future SO users to identify relevant questions. – Maurits Evers Apr 17 '19 at 02:24