Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mutate_at() with negated select helpers e.g(not one_of())

Tags:

r

dplyr

tidyverse

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?

like image 915
G_T Avatar asked Aug 25 '17 06:08

G_T


People also ask

Why mutate_at and transmute_at are not applied to grouping variables?

If applied on a grouped tibble, these operations are not applied to the grouping variables. The behaviour depends on whether the selection is implicit ( all and if selections) or explicit ( at selections). Grouping variables covered by explicit selections in mutate_at () and transmute_at () are always an error.

What does the mutate () function do?

The mutate()function adds new variables to a data frame while preserving any existing variables.  The basic synax for mutate() is as follows: data <- mutate(new_variable = existing_variable/3)

What variables are ignored by mutate_all ()?

Grouping variables covered by implicit selections are ignored by mutate_all (), transmute_all (), mutate_if (), and transmute_if (). The names of the new columns are derived from the names of the input variables and the names of the functions.

How do you call a mutate function from a parenthasis?

So when you use mutate (), you’ll call the function by name. Then the first argument is the dataframe that you want to manipulate. For example, if you had a dataframe named df, that would be the first item inside of the parenthasis (i.e., the first “argument” to the mutate function):


1 Answers

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
like image 88
akrun Avatar answered Oct 25 '22 00:10

akrun