[R] Reshape Dataframe

Bert Jacobs b.jacobs at pandora.be
Thu Dec 20 17:56:23 CET 2007


Hi,

The problem is probably that my Var4, does not contain number but factor
information, and therefore I think Gabor's suggestion does not work.
The same holds for Hadley's solution with the functions melt/cast, where the
resulting dataframe looks OK, but the dataframe is filled with the number of
times Var3 occurred for a certain Var1 and a certain Var2. It looks more
like:

Var1	Var2	W1	W2	W3	W4
A	Fa	1	1	
A	Si	1		1
B	Si	1		
C	La		1
C	Do				2

While it should be:

Var1	Var2	W1	W2	W3	W4
A	Fa	F1	F3	
A	Si	F2		F4
B	Si	F5		
C	La		F6
C	Do				F7
C	Do				F8

Or even better:

Var1	Var2	W1	W2	W3	W4
A	Fa	F1	F3	
A	Si	F2		F4
B	Si	F5		
C	La		F6
C	Do				F7/F8

If my original df is like
Var1	Var2	Var3	Var4
A	Fa	W1	F1
A	Si	W1	F2
A	Fa	W2	F3
A	Si	W3	F4
B	Si	W1	F5
C	La	W2	F6
C	Do	W4	F7
C	Do	W4	F8

-----Original Message-----
From: John Kane [mailto:jrkrideau at yahoo.ca] 
Sent: 19 December 2007 17:57
To: Bert Jacobs; 'hadley wickham'
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Reshape Dataframe

I think if you use Gabor's suggested addtion of
add.missing to the original cast command you get what
you want.

cast(dfm, ... ~ Var3, add.missing=TRUE)

--- Bert Jacobs <b.jacobs at pandora.be> wrote:

> 
> 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")
> 
> Newdf
> 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
> 
> 
> Thx,
> Bert
> 
> -----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?
> 
> library(reshape)
> dfm <- melt(df, id = 1:3)
> cast(dfm, ... ~ Var3)
> 
> You can find out more about the reshape package at
> http://had.co.nz/reshape
> 
> Hadley
> 
> -- 
> http://had.co.nz/
> 
> ______________________________________________
> 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.
> 



      Looking for a X-Mas gift?  Everybody needs a Flickr Pro Account.

 

http://www.flickr.com/gift/



More information about the R-help mailing list