Skip to content

Using related columns in cur_column() with across() #7774

@olivroy

Description

@olivroy

Hello!
I would like to write code with dplyr + across. for this:

Where x = x_1 + x_2, y = y_1 + y+2, etc.

tibble(
  x = c(100, 50, 20),
  x_1 = c(20, 30, 10),
  x_2 = c(80, 20, 10),
  y = c(20, 12, 12),
  y_1 = c(15, 10, 2),
  y_2 = c(5, 2, 10)
) |>
   mutate(
    across(matches("\\d$"), \(column) column / .data[[stringr::str_remove(col_name, "_\\d")
  )

I wonder it would be possible to add a helper like cur_col_name() for example that returns a string that I could manipulate to extract another column name

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions