[R] graph from txt file

Rui Barradas ruipbarradas at sapo.pt
Sat Jun 30 20:12:30 CEST 2012


Hello,

The answer to the question is yes.
But first a note. Your vertex ids start at 51 and the greater is 173. 
igraph vertices (and edges) are automatically numbered starting at 1, in 
this latest package version. Previous versions vertex numbers were zero 
based. If you look online you will almost surely find code with this 
previous behaviour. In your example, this means that you are creating a 
graph with 173 vertices when in fact it only has 5.
So, I've redid the ids 1 to 5 and used your numbers as vertex labels.


dat <- read.csv(text="
id1,id2,label,time
1,2,0,315522000
1,2,0,315522000
3,4,0,315522000
3,5,0,415522000
", header=TRUE)

vertices <- as.vector( t(dat[, 1:2]) )
vlabels <- c(51, 66, 140, 157, 173)

g <- graph(vertices, directed=FALSE)
V(g)$label <- vlabels
E(g)$label <- dat$label
E(g)$time <- as.POSIXct(as.POSIXlt(dat$time, origin="1970-01-01"))

plot(g, layout=layout.circle, edge.label=E(g)$weight, edge.color=E(g)$color)

# Finally, this is what you want.
vert.incident <- c(1, 2)
e <- get.edge.ids(g, vp=vert.incident, directed=FALSE)
E(g)[e]$time

If the egde doesn't exist it returns zero. So, you should test it for 
positiveness to avoid trying to index E(g) with an invalid index number.

Hope this helps,

Rui Barradas

Em 30-06-2012 15:20, HIMANSHU MITTAL escreveu:
> Thanks a lot.
> Just one more question.
> me given the two node ids and the graph, can i find the corresponding
> edge attributes( date and label)?
>
> On Sat, Jun 30, 2012 at 2:10 PM, Rui Barradas <ruipbarradas at sapo.pt
> <mailto:ruipbarradas at sapo.pt>> wrote:
>
>     Hello,
>
>     Just set the attribute,
>
>     V(g)$date <- as.POSIXct(as.POSIXlt(rep(__315522000, 6),
>     origin="1970-01-01"))
>     V(g)$date
>
>
>     Rui Barradas
>
>     Em 30-06-2012 04:26, HIMANSHU MITTAL escreveu:
>
>         Thanks a lot.
>         But i have one more doubt
>         one of the attribute i have is time of edge formation
>
>         id1,id2,label,time
>         51,66,0,315522000
>         51,66,0,315522000
>         140,157,0,315522000
>         140,173,0,415522000
>         so is there any attribute for storing timestamps like for weight
>         or color
>         or if i store it in color would i lose the information?
>
>         On Sat, Jun 30, 2012 at 2:56 AM, Rui Barradas
>         <ruipbarradas at sapo.pt <mailto:ruipbarradas at sapo.pt>
>         <mailto:ruipbarradas at sapo.pt <mailto:ruipbarradas at sapo.pt>>> wrote:
>
>             Hello,
>
>             Package igraph can create graphs. Example:
>
>             dat <- read.table(text="
>             node1  node2  attr1  attr2
>             2  1  2  3
>             3  2  3  2
>             4  3  4  2
>             6  5  1  4
>             ", header=TRUE)
>             dat
>
>             vertices <- as.vector( t(dat[, 1:2]) )
>
>             g <- graph(vertices, directed=FALSE)
>             E(g)$weight <- dat$attr1
>             E(g)$color <- dat$attr2
>
>             plot(g, layout=layout.circle, edge.label=E(g)$weight,
>             edge.color=E(g)$color)
>
>
>             Also, you should post data examples like the posting guide says.
>             With your description, a small example like the one above
>         would do.
>
>             Hope this helps,
>
>             Rui Barradas
>
>             Em 29-06-2012 19:05, HIMANSHU MITTAL escreveu:
>
>                 yes i would prefer igraph, but it can be any r package
>         as long
>                 as it can
>                 create the graph
>
>                 On Fri, Jun 29, 2012 at 11:14 PM, Peter Ehlers
>                 <ehlers at ucalgary.ca <mailto:ehlers at ucalgary.ca>
>         <mailto:ehlers at ucalgary.ca <mailto:ehlers at ucalgary.ca>>> wrote:
>
>                     On 2012-06-29 10:28, HIMANSHU MITTAL wrote:
>
>                         Hi all,
>                         I have a text file in which the graph info is
>         stored as:
>                         node1 node2 attr1 attr2
>                         where there is an edge b/w node1&2 and attr1&2
>         are edge
>                         atttributes
>
>                           is there any way to create a graph using such
>         format in r?
>
>
>                     The igraph package?
>
>                     Peter Ehlers
>
>
>                         Regards,
>                         Himanshu Mittal
>
>                                 [[alternative HTML version deleted]]
>
>                         __________________________________**________________
>         R-help at r-project.org <mailto:R-help at r-project.org>
>         <mailto:R-help at r-project.org <mailto:R-help at r-project.org>>
>                         mailing list
>         https://stat.ethz.ch/mailman/*____*listinfo/r-help
>         <https://stat.ethz.ch/mailman/*__*listinfo/r-help>
>
>           <https://stat.ethz.ch/mailman/__**listinfo/r-help
>         <https://stat.ethz.ch/mailman/**listinfo/r-help>><https://__stat. <https://stat.>__ethz.ch/mailman/__listinfo/r-__help
>         <http://ethz.ch/mailman/listinfo/r-__help>
>
>                         <https://stat.ethz.ch/mailman/__listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>>>
>                         PLEASE do read the posting guide
>         http://www.R-project.org/**
>                         posting-guide.html
>                         <http://www.R-project.org/____posting-guide.html
>         <http://www.R-project.org/__posting-guide.html>
>
>                         <http://www.R-project.org/__posting-guide.html
>         <http://www.R-project.org/posting-guide.html>>>
>
>                         and provide commented, minimal, self-contained,
>                         reproducible code.
>
>
>
>
>                         [[alternative HTML version deleted]]
>
>                 __________________________________________________
>         R-help at r-project.org <mailto:R-help at r-project.org>
>         <mailto:R-help at r-project.org <mailto:R-help at r-project.org>>
>         mailing list
>         https://stat.ethz.ch/mailman/____listinfo/r-help
>         <https://stat.ethz.ch/mailman/__listinfo/r-help>
>
>                 <https://stat.ethz.ch/mailman/__listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>>
>                 PLEASE do read the posting guide
>         http://www.R-project.org/____posting-guide.html
>         <http://www.R-project.org/__posting-guide.html>
>
>                 <http://www.R-project.org/__posting-guide.html
>         <http://www.R-project.org/posting-guide.html>>
>                 and provide commented, minimal, self-contained,
>         reproducible code.
>
>
>
>
>



More information about the R-help mailing list