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?