Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

used by = instead of .by= in mutate, but no error or warning returned #7096

Open
ywhcuhk opened this issue Oct 26, 2024 · 2 comments
Open

used by = instead of .by= in mutate, but no error or warning returned #7096

ywhcuhk opened this issue Oct 26, 2024 · 2 comments

Comments

@ywhcuhk
Copy link

ywhcuhk commented Oct 26, 2024

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     5
@olivroy
Copy link

olivroy commented Oct 28, 2024

by is a valid column name. This is the reason why mutate() uses .by.

@ywhcuhk
Copy link
Author

ywhcuhk commented Oct 30, 2024

Yes, I understand that. I am just thinking a typo of by= is so easy. Maybe it's just me...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants