Hi,


First, a big thanks to all those who replied.


I am including all the replies in one email for easier reference later:

# Input from David
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  reshape(df.1, idvar=1:2, sep="_", direction="long",
varying=names(df.1)[3:8])
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


# Input from Dennis
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  dfr1 <- reshape(df.1, idvar = c("id", "gender"), v.names = c("WMC", "RT"),
                timevar = "type", times = c("alcohol", "caffeine",
"no.drug"),
                varying = list(3:5, 6:8), direction = "long")
  rownames(dfr1) <- NULL
  dfr
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


# Input from Arun
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  library(reshape2)
  library(plyr)
  join_all(lapply(c("WMC","RT"),function(x)
transform(melt(df.1[,c(1:2,grep(x,names(df.1)))],id.vars=c("id","gender"),var="drug"),drug=gsub(".*\\_","",drug))),by=c
 ("id","gender","drug"))
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


Cheers !


On Sat, Nov 30, 2013 at 1:20 AM, David Winsemius <dwinsemius@comcast.net>wrote:

>
> On Nov 29, 2013, at 9:42 AM, Burhan ul haq wrote:
>
> > Hi,
> >
> > This is the input data frame:
> >
> > ###############################################
> > df.1 = read.table(header=T,text="
> > id gender WMC_alcohol WMC_caffeine WMC_no.drug RT_alcohol RT_caffeine
> > RT_no.drug
> > 1 1 female 3.7 3.7 3.9 488 236 371
> > 2 2 female 6.4 7.3 7.9 607 376 349
> > 3 3 female 4.6 7.4 7.3 643 226 412
> > 4 4 male 6.4 7.8 8.2 684 206 252
> > 5 5 female 4.9 5.2 7.0 593 262 439
> > 6 6 male 5.4 6.6 7.2 492 230 464
> > 7 7 male 7.9 7.9 8.9 690 259 327
> > 8 8 male 4.1 5.9 4.5 486 230 305
> > 9 9 female 5.2 6.2 7.2 686 273 327
> > 10 10 female 6.2 7.4 7.8 645 240 498
> >                  ")
> > ###############################################
> >
> > This is the desired output:
> > ###############################################
> > id gender drug WMC RT
> > 1 1 female alcohol 3.7 488
> > 2 2 female alcohol 6.4 607
> > 3 3 female alcohol 4.6 643
> > 4 4 male alcohol 6.4 684
> > 5 5 female alcohol 4.9 593
> > 6 6 male alcohol 5.4 492
> > 7 7 male alcohol 7.9 690
> > 8 8 male alcohol 4.1 486
> > 9 9 female alcohol 5.2 686
> > 10 10 female alcohol 6.2 645
> > 11 1 female caffeine 3.7 236
> > 12 2 female caffeine 7.3 376
> > ###############################################
> >
> > I know some melt and cast magic is required. But I was unable to sort it
> > myself.
> >
> # this is base::reshape
>
> reshape(df.1, idvar=1:2, sep="_", direction="long",
> varying=names(df.1)[3:8])
>
>
> > Here are the dput versions:
> >
> > Input Data Frame
> > ###############################################
> >> dput(df.1)
> > structure(list(id = 1:10, gender = structure(c(1L, 1L, 1L, 2L,
> > 1L, 2L, 2L, 2L, 1L, 1L), .Label = c("female", "male"), class = "factor"),
> >    WMC_alcohol = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2,
> >    6.2), WMC_caffeine = c(3.7, 7.3, 7.4, 7.8, 5.2, 6.6, 7.9,
> >    5.9, 6.2, 7.4), WMC_no.drug = c(3.9, 7.9, 7.3, 8.2, 7, 7.2,
> >    8.9, 4.5, 7.2, 7.8), RT_alcohol = c(488L, 607L, 643L, 684L,
> >    593L, 492L, 690L, 486L, 686L, 645L), RT_caffeine = c(236L,
> >    376L, 226L, 206L, 262L, 230L, 259L, 230L, 273L, 240L), RT_no.drug =
> > c(371L,
> >    349L, 412L, 252L, 439L, 464L, 327L, 305L, 327L, 498L)), .Names =
> > c("id",
> > "gender", "WMC_alcohol", "WMC_caffeine", "WMC_no.drug", "RT_alcohol",
> > "RT_caffeine", "RT_no.drug"), class = "data.frame", row.names = c("1",
> > "2", "3", "4", "5", "6", "7", "8", "9", "10"))
> >
> >
> > Output Data Frame
> > ###############################################
> >> dput(df.output)
> > structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
> > 1L, 2L), gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
> > 1L, 1L, 1L, 1L), .Label = c("female", "male"), class = "factor"),
> >    drug = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> >    2L, 2L), .Label = c("alcohol", "caffeine"), class = "factor"),
> >    WMC = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2,
> >    3.7, 7.3), RT = c(488L, 607L, 643L, 684L, 593L, 492L, 690L,
> >    486L, 686L, 645L, 236L, 376L)), .Names = c("id", "gender",
> > "drug", "WMC", "RT"), class = "data.frame", row.names = c("1",
> > "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))
> >
> >
> > Cheers !
> >
> >       [[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.
>
> David Winsemius
> Alameda, CA, USA
>
>

	[[alternative HTML version deleted]]

