[R] Differences per group

rens_1112 tibidabo at rocketmail.com
Wed Mar 9 16:27:11 CET 2011


Dear all,

Probably a rather stupid question, but I couldn't find the answer..

I currently have a dataframe with customers' id's and purchase amount, what
I would like to do is to show the difference between amount purchased
compared to the month before per customer. 

I have made some attempts myself using diff, but what i can not get to work
is that each different customer starts with a 0.

This is an example of what I have:

>purchase_amount
customer	amount
123		6	
123		5
123		9
123		8
123		11
123		7
230		19
230		18
230		12
230		17
380		7
380		9	
380		2	
380		7
380		8
380		8	
380		6
380		8

How I would like it to look like:

customer	amount	difference
123		6		0	
123		5		-1
123		9		4
123		8		-1
123		11		3
123		7		-4
230		19		0
230		18		-1
230		12		-6
230		17		5
380		7		0
380		9		2
380		2		-7
380		7		5
380		8		1
380		8		0
380		6		-2
380		8		2

I hope my question is clear like this.
Many thanks in advance
Rens

--
View this message in context: http://r.789695.n4.nabble.com/Differences-per-group-tp3343800p3343800.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list