Dear all,
I'm having a few problems trying to reshape a data frame. I tried with
reshape{stats} and melt{reshape} but I was missing something. Any help is
very welcome. Please find details below:
#################################
# data in its original shape:
indiv <- rep(c("A","B"),c(10,10))
level.1 <- rpois(20, lambda=3)
covar.1 <- rlnorm(20, 3, 1)
level.2 <- rpois(20, lambda=3)
covar.2 <- rlnorm(20, 3, 1)
my.dat <- data.frame(indiv,level.1,covar.1,level.2,covar.2)
# the values of level.1 and level.2 represent the number of cases for the
particular
# combination of indiv*level*covar value
# I would like to do two things:
# 1. reshape to long reducing my.dat[,2:5] into two colums "factor"
(levelslevel.1 & level.2)
# and the covariate
# 2. create one new row for each case in level.1 and level.2
# the new reshaped data.frame would should look like this:
# indiv factor covar case.id
# A level.1 4.614105 1
# A level.1 4.614105 2
# A level.2 31.064405 1
# A level.2 31.064405 2
# A level.2 31.064405 3
# A level.2 31.064405 4
# A level.1 19.185784 1
# A level.2 48.455929 1
# A level.2 48.455929 2
# A level.2 48.455929 3
# etc...
#############################
Thank you very much!!
Ahimsa
--
ahimsa campos-arceiz
www.camposarceiz.com
[[alternative HTML version deleted]]
On 2/20/2008 1:14 PM, ahimsa campos-arceiz wrote:> Dear all, > > I'm having a few problems trying to reshape a data frame. I tried with > reshape{stats} and melt{reshape} but I was missing something. Any help is > very welcome. Please find details below: > > ################################# > # data in its original shape: > > indiv <- rep(c("A","B"),c(10,10)) > level.1 <- rpois(20, lambda=3) > covar.1 <- rlnorm(20, 3, 1) > level.2 <- rpois(20, lambda=3) > covar.2 <- rlnorm(20, 3, 1) > my.dat <- data.frame(indiv,level.1,covar.1,level.2,covar.2) > > # the values of level.1 and level.2 represent the number of cases for the > particular > # combination of indiv*level*covar value > > # I would like to do two things: > # 1. reshape to long reducing my.dat[,2:5] into two colums "factor" (levels> level.1 & level.2) > # and the covariate > # 2. create one new row for each case in level.1 and level.2 > > # the new reshaped data.frame would should look like this: > > # indiv factor covar case.id > # A level.1 4.614105 1 > # A level.1 4.614105 2 > # A level.2 31.064405 1 > # A level.2 31.064405 2 > # A level.2 31.064405 3 > # A level.2 31.064405 4 > # A level.1 19.185784 1 > # A level.2 48.455929 1 > # A level.2 48.455929 2 > # A level.2 48.455929 3 > # etc... > > #############################Maybe there is a better way, but this seems to do what you want: ################################# # data in its original shape: indiv <- rep(c("A","B"),c(10,10)) level.1 <- rpois(20, lambda=3) covar.1 <- rlnorm(20, 3, 1) level.2 <- rpois(20, lambda=3) covar.2 <- rlnorm(20, 3, 1) my.dat <- data.frame(indiv,level.1,covar.1,level.2,covar.2) long <- reshape(my.dat, varying = list(c("level.1","level.2"), c("covar.1","covar.2")), timevar="level", idvar="case.id", v.names=c("ncases","covar"), direction="long") newdf <- with(long, data.frame(indiv = rep( indiv, ncases), level = rep( level, ncases), covar = rep( covar, ncases), case.id = rep(case.id, ncases))) The idea is to first reshape() and then rep() each variable ncases times. You can then convert newdf$level into a factor if you like.> Thank you very much!! > > Ahimsa-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Here is a very clumsy way to do it but I think it
works
fact1 <- rep("level.1", length(mydat[,1]))
fact2 <- rep("level.2", length(mydat[,1]))
lels <- c(fact1,fact2)
nams <- c("indiv", "case.id", "covar")
set1 <- mydat[, c(1,2,3)] ; names(set1) <- nams
set2 <- mydat[,c(1, 4,5)] ; names(set2) <- nams
newdata <- cbind(lels, rbind(set1,set2))
mydata <- rbind(newdata[, c(2,1,4,3)], newdata[,
c(2,1,4,3)])
names(mydata) <- c("indiv", "factor", "covar",
"caseid")
mydata[order(mydata$indiv, mydata$caseid,
mydata$factor),]
--- ahimsa campos-arceiz <ahimsa at camposarceiz.com>
wrote:
> Dear all,
>
> I'm having a few problems trying to reshape a data
> frame. I tried with
> reshape{stats} and melt{reshape} but I was missing
> something. Any help is
> very welcome. Please find details below:
>
> #################################
> # data in its original shape:
>
> indiv <- rep(c("A","B"),c(10,10))
> level.1 <- rpois(20, lambda=3)
> covar.1 <- rlnorm(20, 3, 1)
> level.2 <- rpois(20, lambda=3)
> covar.2 <- rlnorm(20, 3, 1)
> my.dat <-
> data.frame(indiv,level.1,covar.1,level.2,covar.2)
>
> # the values of level.1 and level.2 represent the
> number of cases for the
> particular
> # combination of indiv*level*covar value
>
> # I would like to do two things:
> # 1. reshape to long reducing my.dat[,2:5] into two
> colums "factor" (levels> level.1 & level.2)
> # and the covariate
> # 2. create one new row for each case in level.1 and
> level.2
>
> # the new reshaped data.frame would should look like
> this:
>
> # indiv factor covar case.id
> # A level.1 4.614105 1
> # A level.1 4.614105 2
> # A level.2 31.064405 1
> # A level.2 31.064405 2
> # A level.2 31.064405 3
> # A level.2 31.064405 4
> # A level.1 19.185784 1
> # A level.2 48.455929 1
> # A level.2 48.455929 2
> # A level.2 48.455929 3
> # etc...
>
> #############################
>
> Thank you very much!!
>
> Ahimsa
>
>
> --
> ahimsa campos-arceiz
> www.camposarceiz.com
>
> [[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.
>
> # 2. create one new row for each case in level.1 and level.2 > > # the new reshaped data.frame would should look like this: > > # indiv factor covar case.id > # A level.1 4.614105 1 > # A level.1 4.614105 2 > # A level.2 31.064405 1 > # A level.2 31.064405 2 > # A level.2 31.064405 3 > # A level.2 31.064405 4 > # A level.1 19.185784 1 > # A level.2 48.455929 1 > # A level.2 48.455929 2 > # A level.2 48.455929 3 > # etc... > > #############################This works for me. melt(my.dat,id=c("indiv","covar.1","covar.2"))->my.dat.1 names(my.dat.1)[4:5]<-c("level","case.id") melt(my.dat.1,id=c("indiv","level","case.id"))->my.dat.2 Vikas
Possibly Parallel Threads
- fitting a lognormal distribution using cumulative probabilities
- scaling y-axis to relative frequency in multiple histogram (multhist)
- how to apply the function cut( ) to many columns in a data.frame?
- silly, extracting the value of "C" from the results of somers2
- lme4:glmer with nested data