title: Nested pipe chain in dplyr / left_join
body: <p>In the process of trying to get a grouped lag variable (which isn't possible just using <code>lag</code>), the suggested solution was to pull the data out, lag the distinct rows, and then re-join it.</p>
  
  <p>I prefer to do this without creating intermediate objects, and would like to do it in the midst of a chain.  However it doesn't seem to work as I would expect, and the problem seems to be some interaction between using the <code>.</code> and the nested chain inside the left_join.  </p>
  
  
  
  <pre class="lang-r prettyprint-override"><code>require(tidyverse)
  #&gt; Loading required package: tidyverse
  df &lt;- data.frame(Team = c("A", "A", "A", "A", "B", "B", "B", "C", "C", "D", "D"),
                   Date = c("2016-05-10","2016-05-10", "2016-05-10", "2016-05-10",
                            "2016-05-12", "2016-05-12", "2016-05-12",
                            "2016-05-15","2016-05-15",
                            "2016-05-30", "2016-05-30"), 
                   Points = c(1,4,3,2,1,5,6,1,2,3,9)
  )
  
  
  #This works:
  df %&gt;% left_join(x = ., y = df %&gt;% 
                     distinct(Team, Date) %&gt;% 
                     mutate(Date_Lagged = lag(Date)))
  #&gt; Joining, by = c("Team", "Date")
  #&gt;    Team       Date Points Date_Lagged
  #&gt; 1     A 2016-05-10      1        &lt;NA&gt;
  #&gt; 2     A 2016-05-10      4        &lt;NA&gt;
  #&gt; 3     A 2016-05-10      3        &lt;NA&gt;
  #&gt; 4     A 2016-05-10      2        &lt;NA&gt;
  #&gt; 5     B 2016-05-12      1  2016-05-10
  #&gt; 6     B 2016-05-12      5  2016-05-10
  #&gt; 7     B 2016-05-12      6  2016-05-10
  #&gt; 8     C 2016-05-15      1  2016-05-12
  #&gt; 9     C 2016-05-15      2  2016-05-12
  #&gt; 10    D 2016-05-30      3  2016-05-15
  #&gt; 11    D 2016-05-30      9  2016-05-15
  
  #And this works:
  df %&gt;% left_join(x = ., y = .)
  #&gt; Joining, by = c("Team", "Date", "Points")
  #&gt;    Team       Date Points
  #&gt; 1     A 2016-05-10      1
  #&gt; 2     A 2016-05-10      4
  #&gt; 3     A 2016-05-10      3
  #&gt; 4     A 2016-05-10      2
  #&gt; 5     B 2016-05-12      1
  #&gt; 6     B 2016-05-12      5
  #&gt; 7     B 2016-05-12      6
  #&gt; 8     C 2016-05-15      1
  #&gt; 9     C 2016-05-15      2
  #&gt; 10    D 2016-05-30      3
  #&gt; 11    D 2016-05-30      9
  
  #This doesn't work despite the fact that `.` is df.  
  df %&gt;% left_join(x = ., y = . %&gt;% 
                     distinct(Team, Date) %&gt;% 
                     mutate(Date_Lagged = lag(Date)))
  #&gt; Error in UseMethod("tbl_vars"): no applicable method for 'tbl_vars' applied to an object of class "c('fseq', 'function')"
  
  
  
  #Desired output
  distinct(df, Team, Date) %&gt;%
    mutate(Date_Lagged = lag(Date)) %&gt;%
    right_join(., df) %&gt;%
    select(Team, Date, Points, Date_Lagged)
  #&gt; Joining, by = c("Team", "Date")
  #&gt;    Team       Date Points Date_Lagged
  #&gt; 1     A 2016-05-10      1        &lt;NA&gt;
  #&gt; 2     A 2016-05-10      4        &lt;NA&gt;
  #&gt; 3     A 2016-05-10      3        &lt;NA&gt;
  #&gt; 4     A 2016-05-10      2        &lt;NA&gt;
  #&gt; 5     B 2016-05-12      1  2016-05-10
  #&gt; 6     B 2016-05-12      5  2016-05-10
  #&gt; 7     B 2016-05-12      6  2016-05-10
  #&gt; 8     C 2016-05-15      1  2016-05-12
  #&gt; 9     C 2016-05-15      2  2016-05-12
  #&gt; 10    D 2016-05-30      3  2016-05-15
  #&gt; 11    D 2016-05-30      9  2016-05-15
  </code></pre>
  
  <p>Created on 2018-06-12 by the <a href="http://reprex.tidyverse.org" rel="noreferrer">reprex package</a> (v0.2.0).</p>