Reputation: 294
The output I have trouble reading an undirected graph in R, I've tried to import it to R from a text file "graph.txt" which contains a large number of edges (about 1000) but the problem:
R is not reading the whole graph from the text file and it is omitting a large number of the edges I followed the steps I have found in tutorials for R and the related previous questions in this website but I couldn't modify it.
The graph.txt looks like:
383 886
777 915
793 335
386 492
649 421
362 27
690 59
763 926
540 426
172 736
211 368
567 429
782 530
and the functions I used:
library("igraph")
dd<-scan("graph.txt")
gg<-read_graph("graph.txt",directed=FALSE)
gg
the result looks like:
... 763-- 785 326-- 984 946-- 946 326--1103 326-- 592 698--1636 326--1807
+ ... omitted several edges
Any help please to let R import the whole number of edges from the txt file
Thanks in advance
P.S: I am trying to import data as a graph only (not a table or matrix) to calculate the diameter for the graph using the function:
diameter(graph, directed = FALSE, weights = NULL)
Upvotes: 1
Views: 1033
Reputation: 413
What I usually do is import the file first, then convert it to a graph to be safe. So
library(igraph)
f <- read.table(file = "graph.txt")
edges <- c()
for (e in 1:dim(f)[1]){
edges <- append(edges, c(f[e, 1], f[e, 2]))
}
g <- graph(edges, directed = FALSE)
g
#IGRAPH U--- 926 13 --
#+ edges:
#[1] 383--886 777--915 335--793 386--492 421--649 27--362 59--690 763--926 426--540 172--736
#[11] 211--368 429--567 530--782
Upvotes: 2