[R] adjacency list to non-symmetric matrix

arun smartpink111 at yahoo.com
Tue Feb 5 17:36:47 CET 2013


For converting from first dataset to second,

library(reshape2)
#dat1 is data
dcast(dat1,person~group,value.var="group",length)
#  person a b c d
#1   Greg 1 0 0 0
#2   Mary 0 1 0 1
#3    Sam 1 1 1 0
#4    Tom 0 1 1 1
A.K.


----- Original Message -----
From: Sebastian Haunss <sebastian.haunss at uni-bremen.de>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, February 5, 2013 4:07 AM
Subject: [R] adjacency list to non-symmetric matrix

Dear R community,

is there an easy way to convert an adjacency list (or a data-frame) to a non-symmetric matrix?

The adjacency list has the following form:

person group
1 Sam a
2 Sam b
3 Sam c
4 Greg a
5 Tom b
6 Tom c
7 Tom d
8 Mary b
9 Mary d

I need the data in a matrix with persons as rows and groups as columns:

a b c d
Sam 1 1 1 0
Greg 1 0 0 0
Tom 0 1 1 1
Mary 0 1 0 1

I know that there are several possibilities in some of the network packages to convert adjacency lists to symmetric or sparse matrices, but I couldn't find a way to coerce them into affiliation matrices.


kind regards
Sebastian

-- 

PD Dr. Sebastian Haunss
SfB 597 - Transformations of the State
Universität Bremen
Linzer Str. 9a
28359 Bremen
T: +49-(0)421-218-56654
http://shaunss.ipgovernance.eu/

Recently published:
Haunss, Sebastian (2012), “Gewalt und Gewaltlosigkeit in sozialen Bewegungen”, Forschungsjournal Soziale Bewegungen 25(4): 6–16.
Leifeld, Philip, and Sebastian Haunss (2012), “Political Discourse Networks and the Conflict over Software Patents in Europe”, European Journal of Political Research 51(3): 382–409.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list