Search code examples
rdplyracross

Using across function in R


I have a database similar to this one:

data <- tibble(X = c(1,2,3,4), X_2 = c(2,3,4,5),
               Y = c(3,4,5,6), Y_2 = c(4,5,6,7))

I would like to mutate across X and Y so I could sum X with X_2 and Y with Y_2, generating new vars called X_3 and Y_3.

I was trying with something like this:

data %>% 
  mutate(across(c(X,Y), ~ . + ._2 , .names = "{col}_3")) %>% 
  print()

or like this:

data %>% 
  mutate(across(c(X,Y), ~ . + paste0(.,"_2") , .names = "{col}_3")) %>% 
  print()

Obviously neither of those work since I don't know how to define the second variable that has the same name as the one that I use in the across function (but ends with a suffix).

Can anybody help me with this?

Thanks in advance!


Solution

  • To achieve this using across, you can use the cur_column() function within across to dynamically reference the column names. Here's how you can do it:

    Use mutate and across to create the new variables:

    result <- data %>% 
        mutate(across(c(X, Y), 
        ~ . + get(paste0(cur_column(), "_2")), 
        .names = "{col}_3"))