Created
January 26, 2016 21:43
-
-
Save infotroph/7675ccacdfff28cd64ac to your computer and use it in GitHub Desktop.
Controlling new column names from mutate_each?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sample data: 2x2 manipulation, replicated 4x, multiple responses measured repeatedly over time. | |
# The response variables aren't related: y_also doesn't come "after" y_1 in any meaningful sense. | |
df = expand.grid( | |
day=1:3, | |
replicate=1:4, | |
t1=c("ctrl", "more"), | |
t2=c("ambient", "manipulated")) | |
df$y_1 = rnorm(48) | |
df$y_other = runif(48) | |
df$y_also = rlnorm(48) | |
# ...other y-variables here... | |
# Want to compute differences from the baseline condition within each rep/day | |
# This works, but is tedious: | |
(df | |
%>% group_by(day, replicate) | |
%>% mutate( | |
diff_1 = y_1 - y_1[t1 == "ctrl" & t2 == "ambient"], | |
diff_other = y_other - y_other[t1 == "ctrl" & t2 == "ambient"], | |
diff_also = y_also - y_also[t1 == "ctrl" & t2 == "ambient"]) | |
# ... | |
) | |
# near-working mutate_each approach: | |
# Numbers correct, but differences are named as "diff1", "diff2", "diff3", ... | |
# How to produce "diff_1", "diff_other", "diff_also", ... | |
# (preferably without a manual renaming step)? | |
(df | |
%>% group_by(day, replicate) | |
%>% mutate_each( | |
funs=funs(. - .[t1 == "ctrl" & t2 == "ambient"]), | |
diff=starts_with("y"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment