Reputation: 1587
I have data which looks like this:
library(dplyr)
set.seed(123)
df <- data_frame(X1 = rep(LETTERS[1:4], 6),
X2 = rep(1:2, 12),
ref = sample(1:50, 24),
sampl1 = sample(1:50, 24),
var2 = sample(1:50, 24),
meas3 = sample(1:50, 24))
With dplyr
's scooped commands I can edit and create multiple columns at once e.g:
df %>% mutate_if(is.numeric, funs(new = . - ref))
and if I want to do this to only a subset of columns I can use the select
helpers like so:
df %>% mutate_at(vars(one_of(c("X2", "ref"))), funs(new = . - ref))
However in my case I know that my data will always contain the columns X1
, X2
and ref
but would like to subset the data in such a way to mutate only the columns which are NOT X1
, X2
and ref
. These other columns will be variable in number and name but always numeric. I thought I could do something like this:
df %>% mutate_at(vars(!one_of(c("X1", "X2", "ref"))), funs(new = . - ref))
or maybe
df %>% mutate_at(vars(one_of(!names %in% c("X1", "X2", "ref"))), funs(new = . - ref))
But neither work. How do you do negative dplyr select
helpers?
Upvotes: 6
Views: 2346
Reputation: 887048
The one_of
requires -
and not !
df %>%
mutate_at(vars(-one_of(c("X1", "X2", "ref"))), funs(new = . - ref))
# A tibble: 24 x 9
# X1 X2 ref sampl1 var2 meas3 sampl1_new var2_new meas3_new
# <chr> <int> <int> <int> <int> <int> <int> <int> <int>
# 1 A 1 15 33 14 36 18 -1 21
# 2 B 2 39 35 43 1 -4 4 -38
# 3 C 1 20 27 3 23 7 -17 3
# 4 D 2 42 28 21 11 -14 -21 -31
# 5 A 1 44 14 37 18 -30 -7 -26
# 6 B 2 3 7 6 28 4 3 25
# 7 C 1 24 43 25 16 19 1 -8
# 8 D 2 49 39 9 5 -10 -40 -44
# 9 A 1 46 30 45 47 -16 -1 1
#10 B 2 19 50 31 45 31 12 26
Upvotes: 10