-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Description
I mistakenly used by inside mutate instead of .by. But no error or warning were raised. I only found this due to a coincidence when I happen to have two pieces of code (one using by, the other using .by) producing different results.
I understand that when by= is used inside mutate, dplyr would think I am creating a new column called by with values the same as the group variable. There is nothing with the logic. I just thought an warning or something should be raised because it's such an easy mistake to make.
Below I include a quick example
d1 = tibble(x=c(1:6), y=c(rep(1,3), rep(2,3)))
d1 |> mutate(lag_x = lag(x), by=y)
# A tibble: 6 × 4
# x y lag_x by
# 1 1 1 NA 1
# 2 2 1 1 1
# 3 3 1 2 1
# 4 4 2 3 2
# 5 5 2 4 2
# 6 6 2 5 2
d1 |> mutate(lag_x = lag(x), .by=y)
# A tibble: 6 × 3
# x y lag_x
# 1 1 1 NA
# 2 2 1 1
# 3 3 1 2
# 4 4 2 NA
# 5 5 2 4
# 6 6 2 5Metadata
Metadata
Assignees
Labels
No labels