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

Categories

HOME
wcf
iis
nexus
salt
prolog
silverstripe
advanced-custom-fields
jquery-isotope
rules
phpdocumentor2
dropdown
rxandroidble
width
invoices
tk
spreadsheet
greatest-common-divisor
angular-ui-router
django-forms
hybrid-mobile-app
windows-server-2008-r2
smime
interface-builder
vertices
ibm-infosphere
hdinsight
tor
sentence-similarity
terminator
multiple-return-values
nghttp2
traits
http-status-code-406
rjava
traversal
logmein
openvz
margins
casablanca
ng2-datepicker
merge-module
google-font-api
jdedwards
android-seekbar
spring-orm
sqlmap
delphi-xe2
sapjco3
sakai
anorm
service-reference
chilkat
rfc3986
jquery-week-calendar
xtragrid
home-automation
perl5
matchmaking
netsh
wcfserviceclient
moving-average
backbase-portal
worklight-adapters
akka-remoting
flask-potion
office-2013
ios-bluetooth
barcode4j
multiple-databases
zurb-ink
osmosis
ng-storage
has-many
fluxible
rdms
pocket
touchscreen
channels
staf
tabpage
yahoo-pipes
activity-lifecycle
qstring
google-talk
faker
dual-monitor
jquery-sortable
neko
numerical-stability
sbatch
xming
animated
row-value-expression
tool-rec
jhtmlarea
asp.net-optimization
sites
django-grappelli
atomikos
timecodes
twofish
audit-trail
cfstring

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App