Hi:

Here's another approach:

purchase_amount <- transform(purchase_amount,
         diff = with(purchase_amount, ave(amount, customer, FUN =
function(x) c(0, diff(x)))))
purchase_amount

HTH,
Dennis

On Wed, Mar 9, 2011 at 7:27 AM, rens_1112 <tibid...@rocketmail.com> wrote:

> 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.
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to