title: Conditionally rename elements within groups based on previous observations in tidyverse
body: <p>My dataframe contains the following elements:</p>
  
  <p>1) Each <code>user_id</code> may have several <code>order_id</code>
  2) Each <code>order_id</code> can be of 2 types: <code>monthly</code> orders have a cycle of 1 month (30 days) or 3 months (90 days). The users - during his lifetime - can switch from one <code>plan_type</code> to the other multiple times. </p>
  
  <pre><code>library(tidyverse)
  df_input &lt;- tibble::tribble(
                ~user_id, ~order_id,        ~date, ~plan_type,
                       1,       123, "01-01-2020",  "monthly",
                       1,       124, "01-31-2020",  "monthly",
                       1,       125, "03-01-2020", "3-months",
                       2,       126, "01-11-2019", "3-months",
                       2,       127, "10-13-2018",  "monthly",
                       2,       128, "11-12-2018",  "monthly",
                       3,       129, "01-10-2019", "3-months",
                       3,       130, "04-10-2019", "3-months",
                       3,       131, "07-09-2019", "3-months",
                       4,       132, "01-02-2020",  "monthly",
                       4,       133, "02-01-2020",  "monthly"
                )
  
  &gt; df_input
  # A tibble: 11 x 4
     user_id order_id date       plan_type
       &lt;dbl&gt;    &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;    
   1       1      123 01-01-2020 monthly  
   2       1      124 01-31-2020 monthly  
   3       1      125 03-01-2020 3-months 
   4       2      126 01-11-2019 3-months 
   5       2      127 10-13-2018 monthly  
   6       2      128 11-12-2018 monthly  
   7       3      129 01-10-2019 3-months 
   8       3      130 04-10-2019 3-months 
   9       3      131 07-09-2019 3-months 
  10       4      132 01-02-2020 monthly  
  11       4      133 02-01-2020 monthly  
  </code></pre>
  
  <p>I want to create 2 extra columns as follows:</p>
  
  <p>1) <code>order_type</code>: I want to rank the <code>order_ids</code> grouping by <code>user_id</code> and <code>date</code>; after ranking the order by <code>date</code>, I want to name them <code>acquisition</code> if rank = 1 (first order), <code>repeat</code> otherwise.</p>
  
  <p>I managed to do it as follows:</p>
  
  <pre><code>df_input %&gt;%
    group_by(user_id) %&gt;%
    mutate(rank = row_number(date)) %&gt;%
    mutate(order_type = ifelse(rank == '1','acquisition','repeats'))
  </code></pre>
  
  <p>2) I want to create a <code>behavior_type</code> column. I want to flag each <code>order_id</code> as <code>upgrade</code>, <code>downgrade</code> or <code>none</code>.
  If the user goes from <code>monthly</code> to <code>3-months</code> the order is going to be <code>upgrade</code>, if from <code>3-months</code> to <code>monthly</code> a <code>downgrade</code>, <code>none</code> otherwise. </p>
  
  <p>My final dataframe will have to look like:</p>
  
  <pre><code>df_final &lt;- tibble::tribble(
                ~order_id, ~behavior_type,   ~order_type,
                      123,         "none", "acquisition",
                      124,         "none",      "repeat",
                      125,      "upgrade",      "repeat",
                      126,         "none", "acquisition",
                      127,    "downgrade",      "repeat",
                      128,         "none",      "repeat",
                      129,         "none", "acquisition",
                      130,         "none",      "repeat",
                      131,         "none",      "repeat",
                      132,         "none", "acquisition",
                      133,         "none",      "repeat"
                )
  
  &gt; df_final
  # A tibble: 11 x 3
     order_id behavior_type order_type 
        &lt;dbl&gt; &lt;chr&gt;         &lt;chr&gt;      
   1      123 none          acquisition
   2      124 none          repeat     
   3      125 upgrade       repeat     
   4      126 none          acquisition
   5      127 downgrade     repeat     
   6      128 none          repeat     
   7      129 none          acquisition
   8      130 none          repeat     
   9      131 none          repeat     
  10      132 none          acquisition
  11      133 none          repeat     
  </code></pre>
  
  <p>Any help for this second step</p>