[R] Reshape Dataframe

Bert Jacobs b.jacobs at pandora.be
Tue Dec 18 16:31:32 CET 2007

Thx Hadley,
It works, but I need some finetuning.

If I use the following expression:
Newdf <-reshape(df, timevar="Var3", idvar=c("Var1","Var2"),direction="wide")

Var1	Var2	Var3.W1	Var3.W2	Var3.W3	var3.W4
A	Fa	1	  	3		
A	Si	2				4
B	Si	5		
C	La			6
C	Do							7

Is there an option so that for each Var1 all possible combinations of Var2
are listed (i.e. creation of blanco lines).
Is it possible to name the columns with the values of the original Var3
variable, so that the name Var3.W1 changes to W1? 

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


-----Original Message-----
From: hadley wickham [mailto:h.wickham at gmail.com] 
Sent: 18 December 2007 15:16
To: Bert Jacobs
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Reshape Dataframe

On 12/18/07, 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?

dfm <- melt(df, id = 1:3)
cast(dfm, ... ~ Var3)

You can find out more about the reshape package at http://had.co.nz/reshape



More information about the R-help mailing list