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))> Inputpeople 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"))> Outputschool 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]]
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.
Hello, With the following the order of both rows and columns will be different than the order of your example output, but the table is basically the same. xtabs(time ~ people + place, data = Input) Hope this helps, Rui Barradas Em 13-04-2013 22:03, sylvain willart escreveu:> 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. >