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

collect sparkr into dataframe
How does glmnet handle NA values?
R POSIX %H:%M:%S Time Average
Variable that updates conditional value
R code to analyze genotyping data.
Running a Powershell script from R using system2() rather than system()?
Enabling vignette compression for R CMD build in RStudio
What is the NULL environment?
Simple function does not work for `dcast` - reshape2
Use to 2 parameters … in one function
Randomly Assign Integers in R within groups without replacement
why function rbind.data.frame behave different in do.call
Is there in R something like the “here document” in bash?
How to index character vector in file.path in R
R Shiny: Error in max - min : non-numeric argument to binary operator
using a loop in R to output many files

Categories

HOME
redux-thunk
templates
charts
time-series
kali-linux
draftjs
postman
nfc
iccube
transition
http-status-code-404
deprecated
orm
weblate
rbenv
eclipse-pde
computational-geometry
weblogic-10.x
azure-sqldw
code-first
livesearch
high-level
vertices
calculated-columns
preact
pdfjs
apache-spark-ml
bitrix
winbugs
google-analytics-v4
photo
google-trends
biom
php-phantomjs
state-space
mobilefirst-analytics
comet
joomla3.4
bytecode
flake8
python-docx
visualworks
c2664
aws-mobilehub
np
observers
payumoney
bit-fields
mpmediaitem
python-decorators
working-set
taplytics
ftrace
shortcode
google-voice-search
rails-generate
substr
false-positive
pyral
xuggler
appgyver
weibo
pocl
staf
slidify
http-status-code-304
visual-web-developer-2010
boost-process
turing-complete
rackspace
app.yaml
relaycommand
datagridviewrow
openears
boilerplatejs
espn
man-in-the-middle
aslr
credential-providers
array-merge
pppoe
boehm-gc
sharp-architecture
baml

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