Home > OS >  Loop by column name with group_by with dynamic column names in R
Loop by column name with group_by with dynamic column names in R

Time:09-07

I want to calculate the current value - lag(value) by their IDs. My question is how to loop over this procedure by its column names? I want to creat new column names for each calculated value.

data <- data_frame(
  id = sample(letters[1:15], size = 60, replace = TRUE),
  age = round(runif(60, min = 48, max = 90)),
  a = runif(60),
  b = rnorm(60),
  c = rnorm(60),
  d = rnorm(60),
  e = rnorm(60),
  f = rnorm(60),
  g = rnorm(60),
  h = rnorm(60),
  i = rnorm(60),
  j = rnorm(60),
  k = rnorm(60),
  l = rnorm(60))

I want to loop this process from column name "a" to "l" with new column names.

data <- 
  data %>%
  group_by(id) %>%
  arrange(age) %>%
  mutate(a_change = a - lag(a, default = first(a))) 

By desired outcome is to have a_change, b_change, ... l_change.

Thanks!

CodePudding user response:

Use across's names argument to specify the name of new column dynamically ({col} refers to the name of the column) and it will create the desired column names.

library(dplyr)
data %>% 
  group_by(id) %>%
  mutate(across(a:l, ~ .x - lag(.x, default = first(.x)),
                .names = "{col}_change"))
  • Related