Skip to content

feat: implement .before and .after for mutate()#357

Merged
etiennebacher merged 8 commits into
etiennebacher:mainfrom
Yousa-Mirage:feat/mutate-before-after
May 18, 2026
Merged

feat: implement .before and .after for mutate()#357
etiennebacher merged 8 commits into
etiennebacher:mainfrom
Yousa-Mirage:feat/mutate-before-after

Conversation

@Yousa-Mirage
Copy link
Copy Markdown
Contributor

As the title, this PR implemented the .before and .after arguments for mutate(), so all arguments of mutate() are consistent with dplyr now.

During the implementation, I also found a bug of relocate() because .before and .after are based on it. See:

library(dplyr)
library(tidypolars)

df <- tibble::tibble(
  a = 1,
  petal_1 = 2,
  petal_2 = 3,
  z = 4
)
df_pl <- as_polars_df(df)

df |> relocate(z, .before = starts_with("petal"))
#> # A tibble: 1 × 4
#>       a     z petal_1 petal_2
#>   <dbl> <dbl>   <dbl>   <dbl>
#> 1     1     4       2       3
df_pl |> relocate(z, .before = starts_with("petal"))
#> Warning in seq_len(limit): first element used of 'length.out' argument
#> Error in `seq_len()`:
#> ! argument must be coercible to non-negative integer

df |> relocate(z, .before = starts_with("not_found"))
#> # A tibble: 1 × 4
#>       z     a petal_1 petal_2
#>   <dbl> <dbl>   <dbl>   <dbl>
#> 1     4     1       2       3
df_pl |> relocate(z, .before = starts_with("not_found"))
#> Warning in seq_len(limit): first element used of 'length.out' argument
#> Error in `seq_len()`:
#> ! argument must be coercible to non-negative integer

I fixed this bug as well, just change relocate.R from

    limit <- which(names_data == .before) - 1

to

    limit <- if (is.null(.before)) {
      0
    } else {
      which(names_data == .before[1]) - 1
    }

Copy link
Copy Markdown
Owner

@etiennebacher etiennebacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM, there are just minor comments, merge conflicts to fix, and snapshots to update.

Comment thread R/mutate.R Outdated
Comment thread R/mutate.R Outdated
# Conflicts:
#	R/mutate.R
#	tests/testthat/test-mutate-lazy.R
#	tests/testthat/test-mutate.R
@etiennebacher etiennebacher merged commit bad209b into etiennebacher:main May 18, 2026
13 of 15 checks passed
@Yousa-Mirage Yousa-Mirage deleted the feat/mutate-before-after branch May 18, 2026 11:15
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

Successfully merging this pull request may close these issues.

2 participants