rdplyracross

Append across output to data


I am looking for a function to append across function output to input data. Any lead

library(tidyverse)

gdf <-
  tibble(g = c(1, 1, 2, 3), v1 = 10:13, v2 = 20:23) %>%
  group_by(g)

gdf
#> # A tibble: 4 × 3
#> # Groups:   g [3]
#>       g    v1    v2
#>   <dbl> <int> <int>
#> 1     1    10    20
#> 2     1    11    21
#> 3     2    12    22
#> 4     3    13    23

set.seed(1)

# Outside: 1 normal variate
n <- rnorm(1)

gdf %>% 
  mutate(across(v1:v2, ~ .x + n))
#> # A tibble: 4 × 3
#> # Groups:   g [3]
#>       g    v1    v2
#>   <dbl> <dbl> <dbl>
#> 1     1  9.37  19.4
#> 2     1 10.4   20.4
#> 3     2 11.4   21.4
#> 4     3 12.4   22.4

Solution

  • To have across() append columns in mutate, you need to indicate the names of the new columns. You can do this either by putting the function(s) in a named list, which will default to using new names of the form {.col}_{.list_name}, or by using the .names argument:

    gdf |> mutate(across(v1:v2, list(n = ~ .x + n)))
    # # A tibble: 4 × 5
    # # Groups:   g [3]
    #       g    v1    v2  v1_n  v2_n
    #   <dbl> <int> <int> <dbl> <dbl>
    # 1     1    10    20  9.37  19.4
    # 2     1    11    21 10.4   20.4
    # 3     2    12    22 11.4   21.4
    # 4     3    13    23 12.4   22.4
    
    gdf |> mutate(across(v1:v2, \(x) x + n, .names = "my_new_{.col}_n"))
    # # A tibble: 4 × 5
    # # Groups:   g [3]
    #       g    v1    v2 my_new_v1_n my_new_v2_n
    #   <dbl> <int> <int>       <dbl>       <dbl>
    # 1     1    10    20        9.37        19.4
    # 2     1    11    21       10.4         20.4
    # 3     2    12    22       11.4         21.4
    # 4     3    13    23       12.4         22.4