sylvain willart
2013-Apr-13 21:41 UTC
[R] Reshaping Data for bi-partite Network Analysis [SOLVED]
Wow ! so many thanks Arun and Rui works like a charm problem solved 2013/4/13 arun <smartpink111@yahoo.com>> Hi, > Try this; > library(reshape2) > res<-dcast(Input,people~place,value.var="time") > res[is.na(res)]<-0 > res > # people beach home school sport > #1 Joe 5 3 0 1 > #2 Marc 0 4 2 0 > #3 Mary 0 0 4 0 > > #or > xtabs(time~.,Input) > # place > #people beach home school sport > # Joe 5 3 0 1 > # Marc 0 4 2 0 > # Mary 0 0 4 0 > > A.K. > > > > ________________________________ > From: sylvain willart <sylvain.willart@gmail.com> > To: r-help <r-help@r-project.org>; sylvain willart < > sylvain.willart@gmail.com> > Sent: Saturday, April 13, 2013 5:03 PM > Subject: [R] Reshaping Data for bi-partite Network Analysis > > > Hello > > I have a dataset of people spending time in places. But most people don't > hang out in all the places. > > it looks like: > > > Input<-data.frame(people=c("Marc","Marc","Joe","Joe","Joe","Mary"), > + place=c("school","home","home","sport","beach","school"), > + time=c(2,4,3,1,5,4)) > > Input > people place time > 1 Marc school 2 > 2 Marc home 4 > 3 Joe home 3 > 4 Joe sport 1 > 5 Joe beach 5 > 6 Mary school 4 > > In order to import it within R's igraph, I must use graph.incidence(), but > the data needs to be formatted that way: > > > > > Output<-data.frame(school=c(2,0,4),home=c(4,3,0),sport=c(0,1,0),beach=c(0,5,0), > + row.names=c("Marc","Joe","Mary")) > > Output > school home sport beach > Marc 2 4 0 0 > Joe 0 3 1 5 > Mary 4 0 0 0 > > The Dataset is fairly large (couple hundreds of people and places), and I > would very much appreciate if someone could point me to a routine or > function that could transform my Input dataset to the required Output, > > Thank you very much in advance > > Regards > > Sylvain > > PS: sorry for cross-posting this on statnet and then on R help list, but I > received a message from statnet pointing out the question was more related > to general data management than actual network analysis. Which is true > indeed... > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
HI Sylvain,
To get the same order as in "Output"
Input$place<-
factor(Input$place,levels=c("school","home","sport","beach"))
Input$people<-
factor(Input$people,levels=c("Marc","Joe","Mary"))
xtabs(time~.,Input)
#?? place
#people school home sport beach
?# Marc????? 2??? 4???? 0???? 0
? #Joe?????? 0??? 3???? 1???? 5
? #Mary????? 4??? 0???? 0???? 0
A.K.
________________________________
From: sylvain willart <sylvain.willart at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, April 13, 2013 5:41 PM
Subject: Re: [R] Reshaping Data for bi-partite Network Analysis [SOLVED]
Wow !
so many thanks Arun and Rui
works like a charm
problem solved
2013/4/13 arun <smartpink111 at yahoo.com>
Hi,>Try this;
>library(reshape2)
>res<-dcast(Input,people~place,value.var="time")
>res[is.na(res)]<-0
>?res
>#? people beach home school sport
>#1??? Joe???? 5??? 3????? 0???? 1
>#2?? Marc???? 0??? 4????? 2???? 0
>#3?? Mary???? 0??? 0????? 4???? 0
>
>#or
>?xtabs(time~.,Input)
>#????? place
>#people beach home school sport
>?# Joe????? 5??? 3????? 0???? 1
>?# Marc???? 0??? 4????? 2???? 0
>?# Mary???? 0??? 0????? 4???? 0
>
>A.K.
>
>
>
>________________________________
>?From: sylvain willart <sylvain.willart at gmail.com>
>To: r-help <r-help at r-project.org>; sylvain willart
<sylvain.willart at gmail.com>
>Sent: Saturday, April 13, 2013 5:03 PM
>Subject: [R] Reshaping Data for bi-partite Network Analysis
>
>
>
>Hello
>
>I have a dataset of people spending time in places. But most people
don't
>hang out in all the places.
>
>it looks like:
>
>>
Input<-data.frame(people=c("Marc","Marc","Joe","Joe","Joe","Mary"),
>+? ? ? ? ? ? ?
place=c("school","home","home","sport","beach","school"),
>+? ? ? ? ? ? ? time=c(2,4,3,1,5,4))
>> Input
>? people? place time
>1? ?Marc school? ? 2
>2? ?Marc? ?home? ? 4
>3? ? Joe? ?home? ? 3
>4? ? Joe? sport? ? 1
>5? ? Joe? beach? ? 5
>6? ?Mary school? ? 4
>
>In order to import it within R's igraph, I must use graph.incidence(),
but
>the data needs to be formatted that way:
>
>>
>Output<-data.frame(school=c(2,0,4),home=c(4,3,0),sport=c(0,1,0),beach=c(0,5,0),
>+? ? ? ? ? ? ? ? ? ?
row.names=c("Marc","Joe","Mary"))
>> Output
>? ? ?school home sport beach
>Marc? ? ? 2? ? 4? ? ?0? ? ?0
>Joe? ? ? ?0? ? 3? ? ?1? ? ?5
>Mary? ? ? 4? ? 0? ? ?0? ? ?0
>
>The Dataset is fairly large (couple hundreds of people and places), and I
>would very much appreciate if someone could point me to a routine or
>function that could transform my Input dataset to the required Output,
>
>Thank you very much in advance
>
>Regards
>
>Sylvain
>
>PS: sorry for cross-posting this on statnet and then on R help list, but I
>received a message from statnet pointing out the question was more related
>to general data management than actual network analysis. Which is true
>indeed...
>
>??? [[alternative HTML version deleted]]
>
>______________________________________________
>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.
>