[R] help requested

Petr Savicky savicky at cs.cas.cz
Fri Dec 10 19:31:04 CET 2010


On Fri, Dec 10, 2010 at 07:20:55AM -0800, profaar wrote:
> 
> HI friends,
>   I have very lengthy graph data in edge list format. I want to convert it
> into node list format. 
> 
> example:
> EDGE LIST FORMAT
> 1 2 
> 1 3 
> 1 4 
> 1 5
> 2 3 
> 2 4
> 3 2 
> 4 1
> 4 3
> 4  5
> 5 2
> 5 4
> 
> ITS NODE LIST FORMAT SHOULD BE LIKE:
> 1 2 3  4 5
> 2 3 4
> 3 2
> 4 1 3
> 5 2 4
> 
>   Kindly suggest me which package in R provides the support to do my task.

How long the list of egdes is? For not too large lists, consider also

  library(graph)
  G <- new("graphNEL", edgemode="directed")
  G <- addNode(as.character(1:5), G)
  edges <- read.table(file=stdin(), colClasses="character")
1 2
1 3
1 4
1 5
2 3
2 4
3 2
4 1
4 3
4 5
5 2
5 4

  G <- addEdge(from=edges[, 1], to=edges[, 2], G)
  edgeL(G)

  $`1`
  $`1`$edges
  [1] 2 3 4 5
  
  
  $`2`
  $`2`$edges
  [1] 3 4
  
  
  $`3`
  $`3`$edges
  [1] 2
  
  
  $`4`
  $`4`$edges
  [1] 1 3 5
  
  
  $`5`
  $`5`$edges
  [1] 2 4

Very large lists can be handled by unix/linux sort command (if not sorted
already) and by extracting the end-nodes of the edges starting in each node.
In a sorted file, they form blocks of consecutive lines, so a simple text
processing with perl is sufficient.

Petr Savicky.



More information about the R-help mailing list