mutate_all: Mutate multiple columns

Description Usage Arguments Value Grouping variables Naming Life cycle See Also Examples

View source: R/colwise-mutate.R

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("superseded")}

Scoped verbs (_if, _at, _all) have been superseded by the use of across() in an existing verb. See vignette("colwise") for details.

The scoped variants of mutate() and transmute() make it easy to apply the same transformation to multiple variables. There are three variants:

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
mutate_all(.tbl, .funs, ...)

mutate_if(.tbl, .predicate, .funs, ...)

mutate_at(.tbl, .vars, .funs, ..., .cols = NULL)

transmute_all(.tbl, .funs, ...)

transmute_if(.tbl, .predicate, .funs, ...)

transmute_at(.tbl, .vars, .funs, ..., .cols = NULL)

Arguments

.tbl

A tbl object.

.funs

A function fun, a quosure style lambda ~ fun(.) or a list of either form.

...

Additional arguments for the function calls in .funs. These are evaluated only once, with tidy dots support.

.predicate

A predicate function to be applied to the columns or a logical vector. The variables for which .predicate is or returns TRUE are selected. This argument is passed to rlang::as_function() and thus supports quosure-style lambda functions and strings representing function names.

.vars

A list of columns generated by vars(), a character vector of column names, a numeric vector of column positions, or NULL.

.cols

This argument has been renamed to .vars to fit dplyr's terminology and is deprecated.

Value

A data frame. By default, the newly created columns have the shortest names needed to uniquely identify the output. To force inclusion of a name, even when not needed, name the input (see examples for details).

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).

Naming

The names of the new columns are derived from the names of the input variables and the names of the functions.

The .funs argument can be a named or unnamed list. If a function is unnamed and the name cannot be derived automatically, a name of the form "fn#" is used. Similarly, vars() accepts named and unnamed arguments. If a variable in .vars is named, a new column by that name will be created.

Name collisions in the new columns are disambiguated using a unique suffix.

Life cycle

The functions are maturing, because the naming scheme and the disambiguation algorithm are subject to change in dplyr 0.9.0.

See Also

The other scoped verbs, vars()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
iris <- as_tibble(iris)

# All variants can be passed functions and additional arguments,
# purrr-style. The _at() variants directly support strings. Here
# we'll scale the variables `height` and `mass`:
scale2 <- function(x, na.rm = FALSE) (x - mean(x, na.rm = na.rm)) / sd(x, na.rm)
starwars %>% mutate_at(c("height", "mass"), scale2)
# ->
starwars %>% mutate(across(c("height", "mass"), scale2))

# You can pass additional arguments to the function:
starwars %>% mutate_at(c("height", "mass"), scale2, na.rm = TRUE)
starwars %>% mutate_at(c("height", "mass"), ~scale2(., na.rm = TRUE))
# ->
starwars %>% mutate(across(c("height", "mass"), ~ scale2(.x, na.rm = TRUE)))

# You can also supply selection helpers to _at() functions but you have
# to quote them with vars():
iris %>% mutate_at(vars(matches("Sepal")), log)
iris %>% mutate(across(matches("Sepal"), log))

# The _if() variants apply a predicate function (a function that
# returns TRUE or FALSE) to determine the relevant subset of
# columns. Here we divide all the numeric columns by 100:
starwars %>% mutate_if(is.numeric, scale2, na.rm = TRUE)
starwars %>% mutate(across(where(is.numeric), ~ scale2(.x, na.rm = TRUE)))

# mutate_if() is particularly useful for transforming variables from
# one type to another
iris %>% mutate_if(is.factor, as.character)
iris %>% mutate_if(is.double, as.integer)
# ->
iris %>% mutate(across(where(is.factor), as.character))
iris %>% mutate(across(where(is.double), as.integer))

# Multiple transformations ----------------------------------------

# If you want to apply multiple transformations, pass a list of
# functions. When there are multiple functions, they create new
# variables instead of modifying the variables in place:
iris %>% mutate_if(is.numeric, list(scale2, log))
iris %>% mutate_if(is.numeric, list(~scale2(.), ~log(.)))
iris %>% mutate_if(is.numeric, list(scale = scale2, log = log))
# ->
iris %>%
  as_tibble() %>%
  mutate(across(where(is.numeric), list(scale = scale2, log = log)))

# When there's only one function in the list, it modifies existing
# variables in place. Give it a name to instead create new variables:
iris %>% mutate_if(is.numeric, list(scale2))
iris %>% mutate_if(is.numeric, list(scale = scale2))

Example output

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

# A tibble: 87 x 14
   name  height  mass hair_color skin_color eye_color birth_year sex   gender
   <chr>  <dbl> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
 1 LukeNA    NA blond      fair       blue            19   male  mascu2 C-3PO     NA    NA <NA>       gold       yellow         112   none  mascu3 R2-D2     NA    NA <NA>       white, blred             33   none  mascu4 DartNA    NA none       white      yellow          41.9 male  mascu5 LeiaNA    NA brown      light      brown           19   femafemin6 OwenNA    NA brown, grlight      blue            52   male  mascu7 BeruNA    NA brown      light      blue            47   femafemin8 R5-D4     NA    NA <NA>       white, red red             NA   none  mascu9 BiggNA    NA black      light      brown           24   male  mascu10 Obi-NA    NA auburn, wfair       blue-gray       57   male  mascu# … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#   films <list>, vehicles <list>, starships <list>
# A tibble: 87 x 14
   name  height  mass hair_color skin_color eye_color birth_year sex   gender
   <chr>  <dbl> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
 1 LukeNA    NA blond      fair       blue            19   male  mascu2 C-3PO     NA    NA <NA>       gold       yellow         112   none  mascu3 R2-D2     NA    NA <NA>       white, blred             33   none  mascu4 DartNA    NA none       white      yellow          41.9 male  mascu5 LeiaNA    NA brown      light      brown           19   femafemin6 OwenNA    NA brown, grlight      blue            52   male  mascu7 BeruNA    NA brown      light      blue            47   femafemin8 R5-D4     NA    NA <NA>       white, red red             NA   none  mascu9 BiggNA    NA black      light      brown           24   male  mascu10 Obi-NA    NA auburn, wfair       blue-gray       57   male  mascu# … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#   films <list>, vehicles <list>, starships <list>
# A tibble: 87 x 14
   name   height    mass hair_color skin_color eye_color birth_year sex   gender
   <chr>   <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
 1 Luke-0.0678 -0.120  blond      fair       blue            19   male  mascu2 C-3PO -0.212  -0.132  <NA>       gold       yellow         112   none  mascu3 R2-D2 -2.25   -0.385  <NA>       white, blred             33   none  mascu4 Dart0.795   0.228  none       white      yellow          41.9 male  mascu5 Leia-0.701  -0.285  brown      light      brown           19   femafemin6 Owen0.105   0.134  brown, grlight      blue            52   male  mascu7 Beru-0.269  -0.132  brown      light      blue            47   femafemin8 R5-D4 -2.22   -0.385  <NA>       white, red red             NA   none  mascu9 Bigg0.249  -0.0786 black      light      brown           24   male  mascu10 Obi-0.220  -0.120  auburn, wfair       blue-gray       57   male  mascu# … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#   films <list>, vehicles <list>, starships <list>
# A tibble: 87 x 14
   name   height    mass hair_color skin_color eye_color birth_year sex   gender
   <chr>   <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
 1 Luke-0.0678 -0.120  blond      fair       blue            19   male  mascu2 C-3PO -0.212  -0.132  <NA>       gold       yellow         112   none  mascu3 R2-D2 -2.25   -0.385  <NA>       white, blred             33   none  mascu4 Dart0.795   0.228  none       white      yellow          41.9 male  mascu5 Leia-0.701  -0.285  brown      light      brown           19   femafemin6 Owen0.105   0.134  brown, grlight      blue            52   male  mascu7 Beru-0.269  -0.132  brown      light      blue            47   femafemin8 R5-D4 -2.22   -0.385  <NA>       white, red red             NA   none  mascu9 Bigg0.249  -0.0786 black      light      brown           24   male  mascu10 Obi-0.220  -0.120  auburn, wfair       blue-gray       57   male  mascu# … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#   films <list>, vehicles <list>, starships <list>
# A tibble: 87 x 14
   name   height    mass hair_color skin_color eye_color birth_year sex   gender
   <chr>   <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
 1 Luke-0.0678 -0.120  blond      fair       blue            19   male  mascu2 C-3PO -0.212  -0.132  <NA>       gold       yellow         112   none  mascu3 R2-D2 -2.25   -0.385  <NA>       white, blred             33   none  mascu4 Dart0.795   0.228  none       white      yellow          41.9 male  mascu5 Leia-0.701  -0.285  brown      light      brown           19   femafemin6 Owen0.105   0.134  brown, grlight      blue            52   male  mascu7 Beru-0.269  -0.132  brown      light      blue            47   femafemin8 R5-D4 -2.22   -0.385  <NA>       white, red red             NA   none  mascu9 Bigg0.249  -0.0786 black      light      brown           24   male  mascu10 Obi-0.220  -0.120  auburn, wfair       blue-gray       57   male  mascu# … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#   films <list>, vehicles <list>, starships <list>
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1         1.63        1.25          1.4         0.2 setosa 
 2         1.59        1.10          1.4         0.2 setosa 
 3         1.55        1.16          1.3         0.2 setosa 
 4         1.53        1.13          1.5         0.2 setosa 
 5         1.61        1.28          1.4         0.2 setosa 
 6         1.69        1.36          1.7         0.4 setosa 
 7         1.53        1.22          1.4         0.3 setosa 
 8         1.61        1.22          1.5         0.2 setosa 
 9         1.48        1.06          1.4         0.2 setosa 
10         1.59        1.13          1.5         0.1 setosa 
# … with 140 more rows
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1         1.63        1.25          1.4         0.2 setosa 
 2         1.59        1.10          1.4         0.2 setosa 
 3         1.55        1.16          1.3         0.2 setosa 
 4         1.53        1.13          1.5         0.2 setosa 
 5         1.61        1.28          1.4         0.2 setosa 
 6         1.69        1.36          1.7         0.4 setosa 
 7         1.53        1.22          1.4         0.3 setosa 
 8         1.61        1.22          1.5         0.2 setosa 
 9         1.48        1.06          1.4         0.2 setosa 
10         1.59        1.13          1.5         0.1 setosa 
# … with 140 more rows
# A tibble: 87 x 14
   name   height    mass hair_color skin_color eye_color birth_year sex   gender
   <chr>   <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
 1 Luke-0.0678 -0.120  blond      fair       blue          -0.443 male  mascu2 C-3PO -0.212  -0.132  <NA>       gold       yellow         0.158 none  mascu3 R2-D2 -2.25   -0.385  <NA>       white, blred           -0.353 none  mascu4 Dart0.795   0.228  none       white      yellow        -0.295 male  mascu5 Leia-0.701  -0.285  brown      light      brown         -0.443 femafemin6 Owen0.105   0.134  brown, grlight      blue          -0.230 male  mascu7 Beru-0.269  -0.132  brown      light      blue          -0.262 femafemin8 R5-D4 -2.22   -0.385  <NA>       white, red red           NA     none  mascu9 Bigg0.249  -0.0786 black      light      brown         -0.411 male  mascu10 Obi-0.220  -0.120  auburn, wfair       blue-gray     -0.198 male  mascu# … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#   films <list>, vehicles <list>, starships <list>
# A tibble: 87 x 14
   name   height    mass hair_color skin_color eye_color birth_year sex   gender
   <chr>   <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
 1 Luke-0.0678 -0.120  blond      fair       blue          -0.443 male  mascu2 C-3PO -0.212  -0.132  <NA>       gold       yellow         0.158 none  mascu3 R2-D2 -2.25   -0.385  <NA>       white, blred           -0.353 none  mascu4 Dart0.795   0.228  none       white      yellow        -0.295 male  mascu5 Leia-0.701  -0.285  brown      light      brown         -0.443 femafemin6 Owen0.105   0.134  brown, grlight      blue          -0.230 male  mascu7 Beru-0.269  -0.132  brown      light      blue          -0.262 femafemin8 R5-D4 -2.22   -0.385  <NA>       white, red red           NA     none  mascu9 Bigg0.249  -0.0786 black      light      brown         -0.411 male  mascu10 Obi-0.220  -0.120  auburn, wfair       blue-gray     -0.198 male  mascu# … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#   films <list>, vehicles <list>, starships <list>
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <chr>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# … with 140 more rows
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <int>       <int>        <int>       <int> <fct>  
 1            5           3            1           0 setosa 
 2            4           3            1           0 setosa 
 3            4           3            1           0 setosa 
 4            4           3            1           0 setosa 
 5            5           3            1           0 setosa 
 6            5           3            1           0 setosa 
 7            4           3            1           0 setosa 
 8            5           3            1           0 setosa 
 9            4           2            1           0 setosa 
10            4           3            1           0 setosa 
# … with 140 more rows
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <chr>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# … with 140 more rows
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <int>       <int>        <int>       <int> <fct>  
 1            5           3            1           0 setosa 
 2            4           3            1           0 setosa 
 3            4           3            1           0 setosa 
 4            4           3            1           0 setosa 
 5            5           3            1           0 setosa 
 6            5           3            1           0 setosa 
 7            4           3            1           0 setosa 
 8            5           3            1           0 setosa 
 9            4           2            1           0 setosa 
10            4           3            1           0 setosa 
# … with 140 more rows
# A tibble: 150 x 13
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_fn1
          <dbl>       <dbl>        <dbl>       <dbl> <fct>              <dbl>
 1          5.1         3.5          1.4         0.2 setosa            -0.898
 2          4.9         3            1.4         0.2 setosa            -1.14 
 3          4.7         3.2          1.3         0.2 setosa            -1.38 
 4          4.6         3.1          1.5         0.2 setosa            -1.50 
 5          5           3.6          1.4         0.2 setosa            -1.02 
 6          5.4         3.9          1.7         0.4 setosa            -0.535
 7          4.6         3.4          1.4         0.3 setosa            -1.50 
 8          5           3.4          1.5         0.2 setosa            -1.02 
 9          4.4         2.9          1.4         0.2 setosa            -1.74 
10          4.9         3.1          1.5         0.1 setosa            -1.14 
# … with 140 more rows, and 7 more variables: Sepal.Width_fn1 <dbl>,
#   Petal.Length_fn1 <dbl>, Petal.Width_fn1 <dbl>, Sepal.Length_fn2 <dbl>,
#   Sepal.Width_fn2 <dbl>, Petal.Length_fn2 <dbl>, Petal.Width_fn2 <dbl>
# A tibble: 150 x 13
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sc<dbl>       <dbl>        <dbl>       <dbl> <fct>              <dbl>
 1          5.1         3.5          1.4         0.2 setosa            -0.898
 2          4.9         3            1.4         0.2 setosa            -1.14 
 3          4.7         3.2          1.3         0.2 setosa            -1.38 
 4          4.6         3.1          1.5         0.2 setosa            -1.50 
 5          5           3.6          1.4         0.2 setosa            -1.02 
 6          5.4         3.9          1.7         0.4 setosa            -0.535
 7          4.6         3.4          1.4         0.3 setosa            -1.50 
 8          5           3.4          1.5         0.2 setosa            -1.02 
 9          4.4         2.9          1.4         0.2 setosa            -1.74 
10          4.9         3.1          1.5         0.1 setosa            -1.14 
# … with 140 more rows, and 7 more variables: Sepal.Width_scale2 <dbl>,
#   Petal.Length_scale2 <dbl>, Petal.Width_scale2 <dbl>,
#   Sepal.Length_log <dbl>, Sepal.Width_log <dbl>, Petal.Length_log <dbl>,
#   Petal.Width_log <dbl>
# A tibble: 150 x 13
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sc<dbl>       <dbl>        <dbl>       <dbl> <fct>              <dbl>
 1          5.1         3.5          1.4         0.2 setosa            -0.898
 2          4.9         3            1.4         0.2 setosa            -1.14 
 3          4.7         3.2          1.3         0.2 setosa            -1.38 
 4          4.6         3.1          1.5         0.2 setosa            -1.50 
 5          5           3.6          1.4         0.2 setosa            -1.02 
 6          5.4         3.9          1.7         0.4 setosa            -0.535
 7          4.6         3.4          1.4         0.3 setosa            -1.50 
 8          5           3.4          1.5         0.2 setosa            -1.02 
 9          4.4         2.9          1.4         0.2 setosa            -1.74 
10          4.9         3.1          1.5         0.1 setosa            -1.14 
# … with 140 more rows, and 7 more variables: Sepal.Width_scale <dbl>,
#   Petal.Length_scale <dbl>, Petal.Width_scale <dbl>, Sepal.Length_log <dbl>,
#   Sepal.Width_log <dbl>, Petal.Length_log <dbl>, Petal.Width_log <dbl>
# A tibble: 150 x 13
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sc<dbl>       <dbl>        <dbl>       <dbl> <fct>              <dbl>
 1          5.1         3.5          1.4         0.2 setosa            -0.898
 2          4.9         3            1.4         0.2 setosa            -1.14 
 3          4.7         3.2          1.3         0.2 setosa            -1.38 
 4          4.6         3.1          1.5         0.2 setosa            -1.50 
 5          5           3.6          1.4         0.2 setosa            -1.02 
 6          5.4         3.9          1.7         0.4 setosa            -0.535
 7          4.6         3.4          1.4         0.3 setosa            -1.50 
 8          5           3.4          1.5         0.2 setosa            -1.02 
 9          4.4         2.9          1.4         0.2 setosa            -1.74 
10          4.9         3.1          1.5         0.1 setosa            -1.14 
# … with 140 more rows, and 7 more variables: Sepal.Length_log <dbl>,
#   Sepal.Width_scale <dbl>, Sepal.Width_log <dbl>, Petal.Length_scale <dbl>,
#   Petal.Length_log <dbl>, Petal.Width_scale <dbl>, Petal.Width_log <dbl>
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1       -0.898      1.02          -1.34       -1.31 setosa 
 2       -1.14      -0.132         -1.34       -1.31 setosa 
 3       -1.38       0.327         -1.39       -1.31 setosa 
 4       -1.50       0.0979        -1.28       -1.31 setosa 
 5       -1.02       1.25          -1.34       -1.31 setosa 
 6       -0.535      1.93          -1.17       -1.05 setosa 
 7       -1.50       0.786         -1.34       -1.18 setosa 
 8       -1.02       0.786         -1.28       -1.31 setosa 
 9       -1.74      -0.361         -1.34       -1.31 setosa 
10       -1.14       0.0979        -1.28       -1.44 setosa 
# … with 140 more rows
# A tibble: 150 x 9
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sc<dbl>       <dbl>        <dbl>       <dbl> <fct>              <dbl>
 1          5.1         3.5          1.4         0.2 setosa            -0.898
 2          4.9         3            1.4         0.2 setosa            -1.14 
 3          4.7         3.2          1.3         0.2 setosa            -1.38 
 4          4.6         3.1          1.5         0.2 setosa            -1.50 
 5          5           3.6          1.4         0.2 setosa            -1.02 
 6          5.4         3.9          1.7         0.4 setosa            -0.535
 7          4.6         3.4          1.4         0.3 setosa            -1.50 
 8          5           3.4          1.5         0.2 setosa            -1.02 
 9          4.4         2.9          1.4         0.2 setosa            -1.74 
10          4.9         3.1          1.5         0.1 setosa            -1.14 
# … with 140 more rows, and 3 more variables: Sepal.Width_scale <dbl>,
#   Petal.Length_scale <dbl>, Petal.Width_scale <dbl>

dplyr documentation built on June 19, 2021, 1:07 a.m.