Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop by column name with group_by with dynamic column names in R

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

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"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading