*****  To join INSNA, visit  *****

Hey all,

Following this thread, its actually quite easy to use Rpy to get Python to
do all the messy file and text processing and then just call R for the
interesting computations.
(Coincidentally, I've been doing this all weekend).

Attached is a basic commented sample script:
It assumes that you have a file that's tab delimited with the .txt
extension, there is no additional information in the file, the file is

I've done slightly more interesting things with r and rpy, feel free to ask
me about the details. Let me know if its of use.

Take Care,
You'll need to install:
A python module (you can use scipy, too but just installing this one is MUCH

It works on a Mac and hence should work on flavors of linux and unix. It
should certainly work on Windows, too. I ran the code on 88 matrices wih ~
20 vertices in each case: 4 seconds.

--- here's the code ---

# Basic rpy matrix analyzer
# Written by Bernie Hogan, March 5, 2006
# Do what you like with this code.
# Based on Connected Lives Interview Analysis Code:

import string, os
from rpy import *

DIRECTORY = "#paste your directory here (no / at the end)"

def getNetStats(rawNetworkFileAddress):
    outList = [rawNetworkFileAddress]
    filein = open(DIRECTORY + os.sep + rawNetworkFileAddress)

    lis = []
    for i in filein:
        #This takes a line of a file,
        #cuts off the carriage return at the end,
        #and breaks it up at every tab.
        #i.e. if you have a csv, you can just replacce \t with ,
        row = i.strip().split('\t')

        #this line takes every entry in the
        #list and turns it into an integer.
        #if your matrix file has missing values - it will break this code
        row = map(lambda x:int(x), row)

        #adds the row to the matrix
    #turns the matrix in to an array (which is what rpy asks for)
    arr = array(lis)
    #here's the R code, you just call r.
    #and then whatever the function is in r
    st =  "%r" % r.components(arr)
    st =  "%.3f" % r.gden(arr, mode="graph")
    return string.join(outList, "\t")

def getFiles():
    dirList = os.listdir(DIRECTORY)
    output = ''    
    for i in dirList:
        #Its important that the files you want are
        #the only ones with the appropriate file extension
        if i[-3:] ==  "txt":
            output += getNetStats(i) + "\n"
            print i
    #You can change the directory and this
    #file can directly be imported to excel or SPSS etc...
    fileout = open(DIRECTORY + os.sep + "netstats.txt", 'w')


-- end code --

SOCNET is a service of INSNA, the professional association for social
network researchers ( To unsubscribe, send
an email message to [log in to unmask] containing the line
UNSUBSCRIBE SOCNET in the body of the message.