[R] Help with the 'reshape' package

Petr PIKAL petr.pikal at precheza.cz
Tue Aug 30 14:28:26 CEST 2011


Hi
> Hi all,
> 
> I am reading previous posts and guidance on the 'reshape' package in 
order
> to solve the simple problem below. Thinking that this might be very 
> trivial for most of you, I thought there could be a fast solution coming 

> from you guys, and I´d be very thankful for that.
> 
> I have a matrix with dates, companies, prices, just like
> 
> Date   Company   Price
> set-11   A   3
> dez-11   A   3,2
> jan-12   A   3,3
> fev-12   A   2,7
> mar-12   A   2,7
> abr-12   A   2,8
> mai-12   A   2,9
> jun-12   A   3
> jul-12   A   3,1
> mar-12   B   5
> abr-12   B   5,5
> mai-12   B   5,7
> jun-12   B   7
> jul-12   B   6,6
> dez-11   C   1
> jan-12   C   1,1
> fev-12   C   1,11
> mar-12   C   1,2
> abr-12   C   1,3
> 
> and I want to convert it into something like below (missing data can be 
> zeros or NAs)
> 
>       A   B   C
> set-11   3   NA   NA
> dez-11   3,2   NA   1
> jan-12   3,3   NA   1,1
> fev-12   2,7   NA   1,11
> mar-12   2,7   5   1,2
> abr-12   2,8   5,5   1,3
> mai-12   2,9   5,7   NA
> jun-12   3   7   NA
> jul-12   3,1   6,6   NA



You have already melted data (long format). So simply cast

 cast(test, Date~Company)
Using Price as value column.  Use the value argument to cast to override 
this choice
    Date   A   B    C
1 abr-12 2.8 5.5 1.30
2 dez-11 3.2  NA 1.00
3 fev-12 2.7  NA 1.11
4 jan-12 3.3  NA 1.10
5 jul-12 3.1 6.6   NA
6 jun-12 3.0 7.0   NA
7 mai-12 2.9 5.7   NA
8 mar-12 2.7 5.0 1.20
9 set-11 3.0  NA   NA

Shall be OK.

Regards
Petr



> 
> Workspace is attached. Thanks in advance.
> 
> "This message and its attachments may contain confidential and/or 
> privileged information. If you are not the addressee, please, advise the 

> sender immediately by replying to the e-mail and delete this message."
> 
> "Este mensaje y sus anexos pueden contener información confidencial o 
> privilegiada. Si ha recibido este e-mail por error por favor bórrelo y 
> envíe un mensaje al remitente."
> 
> "Esta mensagem e seus anexos podem conter informação confidencial ou 
> privilegiada. Caso não seja o destinatário, solicitamos a imediata 
> notificação ao remetente e exclusão da 
> mensagem."______________________________________________
> 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.



More information about the R-help mailing list