### r

#### Programmatically choosing which variables to put into dplyr pipe

I'm working with datasets (from smartphone experience sampling) where I have to very frequently performed grouped operations (such as find the variability of a measure within each person, or within each day within each person, etc). Typical code might look like the code below, which calculates within-day variability for some variables, then takes the mean of the within-day variability and joins it to the original data. output <- group_by(mydata, id, day) %>% mutate_at(vars(angr, sad, guil, anx, hap), funs(sd(., na.rm = TRUE))) %>% ungroup() %>% group_by(id) %>% summarize_at(vars(angr, sad, guil, anx, hap), funs('var_day_mean' = mean(., na.rm = TRUE))) %>% join(mydata, .) What I want to do is be able to save this as a function so that instead of having to type out angr, sad, guil, anx, hap many times over, I can call this code (and slight variations on it saved as different functions) on a vector of variable names in a string. So the desired functionality is: vars <- c('angr', 'sad', 'guil', 'anx', 'hap') output <- myfunc(vars) Where myfunc performs the piped operations above. I'm aware that there is a vignette for non standard evaluation using dplyr but it's very limited and doesn't cover mutate or most of what I need to do with this use case, so would appreciate any insight. Reproducible example - what I desire is essentially that the below code work, but currently the dplyr pipe cannot take vars as a character vector the way I have input it. data <- data.frame('ID' = rep(1:10, each = 10), 'day' = rep(c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2), 10), 'anx' = rnorm(100), 'sad' = rnorm(100), 'hap' = rnorm(100)) vars = c('anx', 'sad', 'hap') out <- group_by(data, ID, day) %>% mutate_at(vars, funs(sd(., na.rm = TRUE)))

With mutate_at you can simply supply the names of the columns as a vector: mtcars %>% mutate_at(c("mpg", "hp"), funs(mean)) This should do the trick.

### Related Links

How to insert footnotes in ioslides presentations using RMarkdown

Combining rows in a matrix

Least Square Means problems in R

Conditional variable in lmer package for R?

How can I find specific variables have the same names for all countries in original dataset and select them for another new dataset?

R Shiny - Load parameters and data before calling the app

Using “global” variables in function given to apply. We could see them, but could not assign values

Automatic conversion from integer to numeric during integer calculation in R?

Select range ID with the highest score using foverlaps in R

Change border color for gframe widget

double nesting with tidyverse and purrr

Removing date part from timestamp in R

Merge two data.tables where all rows in dt2 are combined with each row in dt1

Extract non intersected element from a string vector in R [closed]

How to divide many columns? [duplicate]

Scraping text with rvest