[R] help requested

Petr Savicky savicky at cs.cas.cz
Sun Dec 12 14:05:30 CET 2010


On Sat, Dec 11, 2010 at 05:11:37AM -0800, profaar wrote:
> hi
>   thanks for your reply. there are around 20000 nodes in my dataset. will it work for conversion from edge list format to node list format? I am using R under Windows XP.
> 

Under Linux, with 20'000 nodes and 10 random edges from each of them, this
took abuot 108 sec (CPU 2.4 GHz). The advantage of this solution is that
there may be further functions in the package graph (see also class?graphNEL),
which could be used in your application. If not, then the conversion itself
may be done more efficiently, for example

  edges <- read.table(file=stdin())
1 2
1 3
1 4
1 5
2 3
2 4
3 2
4 1
4 3
4 5
5 2
5 4

  out1 <- split(edges$V2, edges$V1)
  out1

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

For the example with 20'000 nodes and 10 random edges from each, this 
took about 0.2 sec.

The output out1 is a list of vectors. This may be transformed to
a vector of strings, for example

  out2 <- sapply(out1, paste, collapse=" ")
  cbind(out2) # cbind() is only for a column output

  out2     
1 "2 3 4 5"
2 "3 4"    
3 "2"      
4 "1 3 5"  
5 "2 4"    

and to a text (with a possible file= argument)

  cat(paste(names(out2), out2), sep="\n")

  1 2 3 4 5
  2 3 4
  3 2
  4 1 3 5
  5 2 4

Petr Savicky.



More information about the R-help mailing list