Message-ID: <971536df0712180703v283f86ccl8a1f716ef4a047b5@mail.gmail.com>
Date: 2007-12-18T15:03:53Z
From: Gabor Grothendieck
Subject: Reshape Dataframe
In-Reply-To: <971536df0712180654t741c94f1q6072fc8a0b6120c6@mail.gmail.com>
On Dec 18, 2007 9:54 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>
> On Dec 18, 2007 9:07 AM, Bert Jacobs <b.jacobs at pandora.be> wrote:
> >
> > Hi,
> >
> > I'm having a bit of problems in creating a new dataframe.
> > Below you'll find a description of the current dataframe and of the
> > dataframe that needs to be created.
> > Can someone help me out on this one?
> > Thx in advance.
> > Bert
> >
> > Current Dataframe
> >
> > Var1 Var2 Var3 Var4
> > A Fa W1 1
> > A Si W1 2
> > A Fa W2 3
> > A Si W3 4
> > B Si W1 5
> > C La W2 6
> > C Do W4 7
> >
> > New Dataframe
> >
> > Var1 Var2 W1 W2 W3 W4
> > A Fa 1 3
> > A Si 2 4
> > A La
> > A Do
> > B Fa
> > B Si 5
> > B La
> > B Do
> > C Fa
> > C Si
> > C La 6
> > C Do 7
>
> Try this:
>
> out <- ftable(xtabs(Var4 ~ Var1 + Var2 + Var3, DF))
> out[out == 0] <- NA
>
> Omit the last line is 0 fill is what you had wanted.
>
> This will do it except that it will eliminate all rows
> without data:
>
> out2 <- reshape(DF, dir = "wide", timevar = "Var3", idvar = c("Var1", "Var2"))
> out2[is.na(out2)] <- 0
>
> Omit the last line if NA fill is what you wanted.
>
> The reshape package melt/cast routines (see Hadley's solution in this
> thread) can be used
> to give a similar result to the reshape command above (i.e. all
> missing rows are not
> included) except that cast is a bit more flexible since it has a fill= argument.
Just one correction. The cast function in reshape has an add.missing= argument
that can control this so actually any of the solutions could be
obtained with cast
using the fill= and add.missing= arguments to control which one you want.
>