Nested pipe chain in dplyr / left_join
Though this isn't an answer to my question (Onyambo provided that!), I wanted to share that I found an alternative way to accomplish the same thing. Basically you use group_by()
and nest()
to squish the tibble and get the repeated vars out of the way, do the lag, and then unnest()
.
df %>%
group_by(Team, Date) %>%
nest() %>%
mutate(Date_Lagged = lag(Date)) %>%
unnest()
#> # A tibble: 11 x 4
#> Team Date Date_Lagged Points
#> <fct> <fct> <fct> <dbl>
#> 1 A 2016-05-10 <NA> 1
#> 2 A 2016-05-10 <NA> 4
#> 3 A 2016-05-10 <NA> 3
#> 4 A 2016-05-10 <NA> 2
#> 5 B 2016-05-12 2016-05-10 1
#> 6 B 2016-05-12 2016-05-10 5
#> 7 B 2016-05-12 2016-05-10 6
#> 8 C 2016-05-15 2016-05-12 1
#> 9 C 2016-05-15 2016-05-12 2
#> 10 D 2016-05-30 2016-05-15 3
#> 11 D 2016-05-30 2016-05-15 9
Created on 2018-06-14 by the reprex package (v0.2.0).
For your code to work, you will need a curly brace around the y
argument as shown below
df %>% left_join(x = ., y = {.} %>%
distinct(Team, Date) %>%
mutate(Date_Lagged = lag(Date)))
Joining, by = c("Team", "Date")
Team Date Points Date_Lagged
1 A 2016-05-10 1 <NA>
2 A 2016-05-10 4 <NA>
3 A 2016-05-10 3 <NA>
4 A 2016-05-10 2 <NA>
5 B 2016-05-12 1 2016-05-10
6 B 2016-05-12 5 2016-05-10
7 B 2016-05-12 6 2016-05-10
8 C 2016-05-15 1 2016-05-12
9 C 2016-05-15 2 2016-05-12
10 D 2016-05-30 3 2016-05-15
11 D 2016-05-30 9 2016-05-15
oe you can just do
df %>% left_join(df%>%
distinct(Team, Date) %>%
mutate(Date_Lagged = lag(Date)))
If you don't mind swapping pipe nesting for function nesting, this accomplishes your goal:
df %>% left_join(mutate(distinct(., Team, Date), Date_Lagged = lag(Date)))
output:
Joining, by = c("Team", "Date")
Team Date Points Date_Lagged
1 A 2016-05-10 1 <NA>
2 A 2016-05-10 4 <NA>
3 A 2016-05-10 3 <NA>
4 A 2016-05-10 2 <NA>
5 B 2016-05-12 1 2016-05-10
6 B 2016-05-12 5 2016-05-10
7 B 2016-05-12 6 2016-05-10
8 C 2016-05-15 1 2016-05-12
9 C 2016-05-15 2 2016-05-12
10 D 2016-05-30 3 2016-05-15
11 D 2016-05-30 9 2016-05-15