Question

In R I'm trying to map all Madrid tube stations using igraph and then calculate the shortest route between two stations (just the number of stations, not the distance). I'm following this syntax: "An undirected graph with two vertices called ‘A’ and ‘B’ and one edge only: graph.formula(A-B)"

Below I just copy two tube lines for clarity's sake.

library("igraph")   
metro<- graph.formula(PinardeChamartin-Bambu-Chamartin-PlazadeCastilla-Valdeacederas-Tetuan-Estrecho-Alvarado-CuatroCaminos-RiosRosas-Iglesia-Bilbao-Tribunal-GranVia-Sol-TirsodeMolina-AntonMartin-Atocha-AtochaRenfe-MenendezPelayo-Pacifico-PuentedeVallecas-NuevaNumancia-Portazgo,LasRosas-AvenidadeGuadalajara-Alsacia-LaAlmudena-LaElipa-Ventas-ManuelBecerra-Goya-PrincipedeVergara-Retiro-BancodeEspana-Sevilla-Sol-Opera-SantoDomingo-Noviciado-SanBernardo-Quevedo-Canal-CuatroCaminos)  
sp <- get.shortest.paths(metro,from="Canal",to="Chamartin")  
V(metro)[sp[[1]]]

It seems to work but I have two question:
1. How can I input the tube stations (nodes) and their relationships A-B for long lists into the graph more efficiently, reading a csv for instance?

2.How can I rename those nodes to include tildes, spaces and "ñ"? Because I tried double quotes before and after each node's name but I get an error. A + sign. I haver checked the long string many times and I cannot see the error, no parenthesis missing.
Sorry if they're very basic questions. I'm a very novice user.
Thank you very much

Was it helpful?

Solution

For the first question, see ?graph.data.frame and ?read.csv.

I am not quite sure what you are asking in the second question, what is the error you are getting. Your code works fine for me, with the modification required for igraph 0.7.x:

V(metro)[sp$vpath[[1]]]
# Vertex sequence:
# [1] "Canal"           "CuatroCaminos"   "Alvarado"        "Estrecho"       
# [5] "Tetuan"          "Valdeacederas"   "PlazadeCastilla" "Chamartin"      
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top