[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Basic question about data importation

From: Tamas Nepusz
Subject: Re: [igraph] Basic question about data importation
Date: Fri, 07 Jan 2011 17:13:08 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20101208 Lightning/1.0b2 Thunderbird/3.1.7

> I am using igraph from R.
I'm sure there's an easier way for this in R, but I'm no R expert, and
this is what I could come up with.

First, I'd read the file using scan() because I never really understood
how data frames should be indexed and I couldn't make this thing work
using data frames:

data <- scan("alliance_list.csv", what="string", sep="\n", skip=1)

data now contains every line of the file in a list. Now I split every
line around the commas, throw away the empty items and enumerate every
edge within a clique:

data <- lapply(strsplit(data, ","), function(row)
as.vector(combn(row[row != ""], 2)))

Now all you need to do is to turn your data list into an nx2 matrix:

data <- matrix(unlist(data), nc=2)

and construct a graph out of it:

g <- graph.edgelist(data, directed=F)

V(g)$name then gives you the names of the companies.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]