0

Please consider:

require(dplyr)

my_data <- 
  data.frame(a.x = sample(1100),
             b.x = sample(1100),
             a.y = sample(1100),
             b.x = sample(1100))

my_data %>%
  mutate(a = a.x + a.y,
         b = b.x + b.y)

Is it possible to pass functions on sets of columns based on their common prefix with dplyr::mutate_at .vars and .funs?

CptNemo
  • 6,455
  • 16
  • 58
  • 107

1 Answers1

0

The dplyr library allows one to apply a function (or a set of functions) to a set of columns. The below example adds a sum column of columns that begin with "a" and adds a standard deviation column for columns that begin with "b":

my_data %>%
  rowwise() %>%
  mutate(
    sum = sum(c_across(starts_with("a"))),
    sd = sd(c_across(starts_with("b")))
    )
Blue050205
  • 276
  • 2
  • 4