In the process of trying to get a grouped lag variable (which isn't possible just using lag
), the suggested solution was to pull the data out, lag the distinct rows, and then re-join it.
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 .
and the nested chain inside the left_join.
require(tidyverse)
#> Loading required package: tidyverse
df <- 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 %>% left_join(x = ., y = df %>%
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
#And this works:
df %>% left_join(x = ., y = .)
#> Joining, by = c("Team", "Date", "Points")
#> Team Date Points
#> 1 A 2016-05-10 1
#> 2 A 2016-05-10 4
#> 3 A 2016-05-10 3
#> 4 A 2016-05-10 2
#> 5 B 2016-05-12 1
#> 6 B 2016-05-12 5
#> 7 B 2016-05-12 6
#> 8 C 2016-05-15 1
#> 9 C 2016-05-15 2
#> 10 D 2016-05-30 3
#> 11 D 2016-05-30 9
#This doesn't work despite the fact that `.` is df.
df %>% left_join(x = ., y = . %>%
distinct(Team, Date) %>%
mutate(Date_Lagged = lag(Date)))
#> 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) %>%
mutate(Date_Lagged = lag(Date)) %>%
right_join(., df) %>%
select(Team, Date, Points, Date_Lagged)
#> 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
Created on 2018-06-12 by the reprex package (v0.2.0).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With