data science

From nCol to Pajek 2/2 – Python & R

Continues from here.

I just found that the Python script in the previous post breaks when it encounters non-alphanumerical characters. That means I had to run a small code through Vim to delete them on both the usrIDs.txt and output.txt files.

%s/[^a-zA-Z0-9_: ]//g

Once that was done then I had to change the usrIDs.txt file to add the quotation marks needed in Pajek, running this through Vim

%s/\( \|$\)/&"/g

so that it goes form this

1 Andy
2 Elle
3 Jim
4 Shaniqua

to this:

1 "Andy"
2 "Elle"
3 "Jim"
4 "Shaniqua"

Then we copypasta all the things into a new file we can call (apparently R can only handle .net).

The *Vertices section is taken from the usrIDs.txt file, and the *Edges section is taken from the output.txt file.

*Vertices 4
1 "Andy"
2 "Elle"
3 "Jim"
4 "Shaniqua"
3 2
1 4
4 2

The file is then loaded on to R


Then a quick R function for some basic graph settings.

draw.quick.graph <- function(g) {

Finally, plot the graph


We’re done!!

Here’s one I made earlier.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s