[R] RES: Help with the 'reshape' package

Filipe Leme Botelho filipe.botelho at vpar.com.br
Tue Aug 30 16:53:08 CEST 2011


Petr, many thanks. It´s very straightforward to to deal with the package, I had different sources of revenues for same company, same month, like below, and just by adding "sum" at the end of the function it collapsed values from all sources. Very useful! Cheers

cast(dta, Data~Company, value="Revenues", sum)

> Date   	Company   	Revenues
> 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
> dez-11   	A  		0,2
> jan-12   	A   		4,3
> fev-12   	A   		2,1
> mar-12   	A   		2,2

	A	B	C
01/09/2011	3	0	0
01/12/2011	3,4	0	1
01/01/2012	7,6	0	1,1
01/02/2012	4,8	0	1,11
01/03/2012	4,9	5	1,2
01/04/2012	2,8	5,5	1,3
01/05/2012	2,9	5,7	0
01/06/2012	3	7	0
01/07/2012	3,1	6,6	0

-----Mensagem original-----
De: Petr PIKAL [mailto:petr.pikal at precheza.cz] 
Enviada em: terça-feira, 30 de agosto de 2011 09:28
Para: Filipe Leme Botelho
Cc: r-help at r-project.org
Assunto: Re: [R] Help with the 'reshape' package

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.


"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."



More information about the R-help mailing list