Dear All, I have some data in the following shape: ID begin_t1 end_t1 begin_t2 end_t2 Thomas 11/03/04 13/05/06 04/02/07 16/05/08 ... ... ... ... ... Jens 24/01/02 23/05/03 07/06/03 14/11/05 I would like to reshape this data to have the following form: ID Begin_Time End_Time Thomas 11/03/04 13/05/06 Thomas 04/02/07 16/05/08 ... ... ... Jens 24/01/02 23/05/03 Jens 07/06/03 14/11/05 I have been doing some google searches and looked at the reshape library, but so far I have not been able to shape the data like I want. If you guys could help, I would greatly appreciate it! Best, Thomas
Hi: Here's one solution using function reshape() in the stats package (adapted from an R-help solution by Thomas Lumley on Nov. 26, 2002): d <- read.table(textConnection(" + ID begin_t1 end_t1 begin_t2 end_t2 + Thomas 11/03/04 13/05/06 04/02/07 16/05/08 + Jens 24/01/02 23/05/03 07/06/03 14/11/05"), + header = TRUE)> dID begin_t1 end_t1 begin_t2 end_t2 1 Thomas 11/03/04 13/05/06 04/02/07 16/05/08 2 Jens 24/01/02 23/05/03 07/06/03 14/11/05 d2 <- reshape(d, varying = list(c('begin_t1', 'begin_t2'), c('end_t1', 'end_t2')), v.names = c('Begin_Time', 'End_Time'), idvar = 'ID', direction 'long') # Result could use some cleanup: rownames(d2) <- NULL # clear the created row names d2 <- d2[order(d2$ID), -2] # sort by name, removing the time order (variable 2) d2 ID Begin_Time End_Time 2 Jens 24/01/02 23/05/03 4 Jens 07/06/03 14/11/05 1 Thomas 11/03/04 13/05/06 3 Thomas 04/02/07 16/05/08 HTH, Dennis On Mon, Jul 19, 2010 at 3:48 PM, Thomas Jensen < thomas.jensen@eup.gess.ethz.ch> wrote:> Dear All, > > I have some data in the following shape: > > ID begin_t1 end_t1 begin_t2 > end_t2 > Thomas 11/03/04 13/05/06 04/02/07 16/05/08 > ... ... ... ... > ... > Jens 24/01/02 23/05/03 07/06/03 14/11/05 > > I would like to reshape this data to have the following form: > > ID Begin_Time End_Time > Thomas 11/03/04 13/05/06 > Thomas 04/02/07 16/05/08 > ... ... ... > Jens 24/01/02 23/05/03 > Jens 07/06/03 14/11/05 > > I have been doing some google searches and looked at the reshape library, > but so far I have not been able to shape the data like I want. If you guys > could help, I would greatly appreciate it! > > Best, Thomas > > ______________________________________________ > 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]]
Assuming your data is in data.frame xx ===========================================================library(reshape) mm1 <- melt(xx, id=c("ID")) cast(mm1, ID ~ variable ) ============================================================ --- On Mon, 7/19/10, Thomas Jensen <thomas.jensen at eup.gess.ethz.ch> wrote:> From: Thomas Jensen <thomas.jensen at eup.gess.ethz.ch> > Subject: [R] Reshaping data > To: R-help at r-project.org > Received: Monday, July 19, 2010, 6:48 PM > Dear All, > > I have some data in the following shape: > > ID??? ??? ??? > begin_t1??? end_t1??? > ??? begin_t2??? end_t2 > Thomas??? ??? > 11/03/04??? 13/05/06??? > 04/02/07??? 16/05/08 > ...??? ??? ??? > ...??? ??? ??? > ...??? ??? ??? > ...??? ??? ??? > ... > Jens??? ??? > 24/01/02??? 23/05/03??? > 07/06/03??? 14/11/05 > > I would like to reshape this data to have the following > form: > > ID??? ??? ??? > Begin_Time??? ??? End_Time > Thomas??? ??? > 11/03/04??? ??? 13/05/06 > Thomas??? ??? > 04/02/07??? ??? 16/05/08 > ...??? ??? ??? > ...??? ??? ??? > ??? ... > Jens??? ??? > 24/01/02??? ??? 23/05/03 > Jens??? ??? > 07/06/03??? ??? 14/11/05 > > I have been doing some google searches and looked at the > reshape library, but so far I have not been able to shape > the data like I want. If you guys could help, I would > greatly appreciate it! > > Best, Thomas > > ______________________________________________ > 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. >
On Tue, Jul 20, 2010 at 3:30 AM, John Kane <jrkrideau at yahoo.ca> wrote:> Assuming your data is in data.frame xx > > ===========================================================> library(reshape) > mm1 <- melt(xx, id=c("ID")) > cast(mm1, ID ?~ variable ) > ============================================================That code just goes in a circle! the result of cast(mm1, ID ~ variable) is equal to xx. Here is one way to do it with the melt/cast functions from the reshape package: m.dat <- melt(dat, id = "ID") m.dat <- cbind(m.dat, colsplit(m.dat$variable, split = "_", names c("begin.end","t"))) m.dat$variable <- NULL dat.final <- cast(m.dat, ... ~ begin.end) Hope it helps, Ista> > --- On Mon, 7/19/10, Thomas Jensen <thomas.jensen at eup.gess.ethz.ch> wrote: > >> From: Thomas Jensen <thomas.jensen at eup.gess.ethz.ch> >> Subject: [R] Reshaping data >> To: R-help at r-project.org >> Received: Monday, July 19, 2010, 6:48 PM >> Dear All, >> >> I have some data in the following shape: >> >> ID >> begin_t1??? end_t1 >> ??? begin_t2??? end_t2 >> Thomas >> 11/03/04??? 13/05/06 >> 04/02/07??? 16/05/08 >> ... >> ... >> ... >> ... >> ... >> Jens >> 24/01/02??? 23/05/03 >> 07/06/03??? 14/11/05 >> >> I would like to reshape this data to have the following >> form: >> >> ID >> Begin_Time??? ??? End_Time >> Thomas >> 11/03/04??? ??? 13/05/06 >> Thomas >> 04/02/07??? ??? 16/05/08 >> ... >> ... >> ??? ... >> Jens >> 24/01/02??? ??? 23/05/03 >> Jens >> 07/06/03??? ??? 14/11/05 >> >> I have been doing some google searches and looked at the >> reshape library, but so far I have not been able to shape >> the data like I want. If you guys could help, I would >> greatly appreciate it! >> >> Best, Thomas >> >> ______________________________________________ >> 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. >> > > > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org