title: Calculating Inter-purchase Time in R
body: <p>I have the following data frame:</p>
  
  <pre><code>id&lt;-c(1,1,1,3,3,3)
  date&lt;-c("23-01-07","27-01-07","30-01-07","11-12-07","12-12-07","01-01-08")
  df&lt;-data.frame(id,date)
  df$date2&lt;-as.Date(as.character(df$date), format = "%d-%m-%y")
  
  
  id    date      date2
  1 23-01-07 2007-01-23
  1 27-01-07 2007-01-27
  1 30-01-07 2007-01-30
  3 11-12-07 2007-12-11
  3 12-12-07 2007-12-12
  3 01-01-08 2008-01-01
  </code></pre>
  
  <p>Now I need to calculate the inter-purchase time of transactions for each id (the number of days between each transaction of a customer and the previous transaction by the same customer); so that I get the following result:</p>
  
  <pre><code>id    date      date2  interpurchase.time
  1 23-01-07 2007-01-23         0
  1 27-01-07 2007-01-27         4 
  1 30-01-07 2007-01-30         3
  3 11-12-07 2007-12-11         0  
  3 12-12-07 2007-12-12         1 
  3 01-01-08 2008-01-01        20
  </code></pre>
  
  <p>I wonder if anyone can help me with this.</p>