當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R dplyr mutate_all 改變多列


[Superseded]

作用域動詞( _if_at_all )已被現有動詞中的 pick()across() 取代。有關詳細信息,請參閱vignette("colwise")

mutate()transmute()scoped 變體可以輕鬆地將相同的轉換應用於多個變量。有以下三種變體:

  • _all 影響每個變量

  • _at 影響使用字符向量或 vars() 選擇的變量

  • _if 影響使用謂詞函數選擇的變量:

用法

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)

參數

.tbl

tbl 對象。

.funs

函數 fun 、 quosure 樣式 lambda ~ fun(.) 或任一形式的列表。

...

.funs 中函數調用的附加參數。這些僅在 tidy dots 支持下評估一次。

.predicate

應用於列或邏輯向量的謂詞函數。選擇.predicate 為或返回TRUE 的變量。該參數傳遞給rlang::as_function(),因此支持quosure-style lambda 函數和表示函數名稱的字符串。

.vars

vars() 生成的列列表、列名稱的字符向量、列位置的數值向量或 NULL

.cols

此參數已重命名為 .vars 以符合 dplyr 的術語,並且已棄用。

一個 DataFrame 。默認情況下,新創建的列具有唯一標識輸出所需的最短名稱。要強製包含名稱(即使不需要),請為輸入命名(有關詳細信息,請參閱示例)。

對變量進行分組

如果應用於分組的 tibble,則這些操作不會應用於分組變量。該行為取決於選擇是隱式的(allif 選擇)還是顯式的(at 選擇)。

  • mutate_at()transmute_at() 中的顯式選擇所覆蓋的變量進行分組始終是一個錯誤。將 -group_cols() 添加到 vars() 選擇中以避免出現這種情況:

    data %>% mutate_at(vars(-group_cols(), ...), myoperation)

    或者從列名的字符向量中刪除group_vars()

    nms <- setdiff(nms, group_vars(data))
    data %>% mutate_at(vars, myoperation)
  • mutate_all()transmute_all()mutate_if()transmute_if() 忽略隱式選擇覆蓋的分組變量。

命名

新列的名稱源自輸入變量的名稱和函數的名稱。

  • 如果隻有一個未命名函數(即,如果 .funs 是長度為 1 的未命名列表),則使用輸入變量的名稱來命名新列;

  • 對於 _at 函數,如果隻有一個未命名變量(即,如果 .vars 的形式為 vars(a_single_column) )並且 .funs 的長度大於 1,則使用函數的名稱來命名新列;

  • 否則,通過連接輸入變量的名稱和函數的名稱來創建新名稱,並用下劃線 "_" 分隔。

.funs 參數可以是命名或未命名列表。如果函數未命名並且無法自動派生名稱,則使用 "fn#" 形式的名稱。同樣,vars() 接受命名和未命名參數。如果 .vars 中的變量被命名,則會創建一個具有該名稱的新列。

新列中的名稱衝突使用唯一的後綴消除歧義。

也可以看看

例子

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)
#> # A tibble: 87 × 14
#>    name      height  mass hair_color skin_color eye_color birth_year sex  
#>    <chr>      <dbl> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke Sky…     NA    NA blond      fair       blue            19   male 
#>  2 C-3PO         NA    NA NA         gold       yellow         112   none 
#>  3 R2-D2         NA    NA NA         white, bl… red             33   none 
#>  4 Darth Va…     NA    NA none       white      yellow          41.9 male 
#>  5 Leia Org…     NA    NA brown      light      brown           19   fema…
#>  6 Owen Lars     NA    NA brown, gr… light      blue            52   male 
#>  7 Beru Whi…     NA    NA brown      light      blue            47   fema…
#>  8 R5-D4         NA    NA NA         white, red red             NA   none 
#>  9 Biggs Da…     NA    NA black      light      brown           24   male 
#> 10 Obi-Wan …     NA    NA auburn, w… fair       blue-gray       57   male 
#> # ℹ 77 more rows
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>
# ->
starwars %>% mutate(across(c("height", "mass"), scale2))
#> # A tibble: 87 × 14
#>    name      height  mass hair_color skin_color eye_color birth_year sex  
#>    <chr>      <dbl> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke Sky…     NA    NA blond      fair       blue            19   male 
#>  2 C-3PO         NA    NA NA         gold       yellow         112   none 
#>  3 R2-D2         NA    NA NA         white, bl… red             33   none 
#>  4 Darth Va…     NA    NA none       white      yellow          41.9 male 
#>  5 Leia Org…     NA    NA brown      light      brown           19   fema…
#>  6 Owen Lars     NA    NA brown, gr… light      blue            52   male 
#>  7 Beru Whi…     NA    NA brown      light      blue            47   fema…
#>  8 R5-D4         NA    NA NA         white, red red             NA   none 
#>  9 Biggs Da…     NA    NA black      light      brown           24   male 
#> 10 Obi-Wan …     NA    NA auburn, w… fair       blue-gray       57   male 
#> # ℹ 77 more rows
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>

# You can pass additional arguments to the function:
starwars %>% mutate_at(c("height", "mass"), scale2, na.rm = TRUE)
#> # A tibble: 87 × 14
#>    name    height    mass hair_color skin_color eye_color birth_year sex  
#>    <chr>    <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke … -0.0678 -0.120  blond      fair       blue            19   male 
#>  2 C-3PO  -0.212  -0.132  NA         gold       yellow         112   none 
#>  3 R2-D2  -2.25   -0.385  NA         white, bl… red             33   none 
#>  4 Darth…  0.795   0.228  none       white      yellow          41.9 male 
#>  5 Leia … -0.701  -0.285  brown      light      brown           19   fema…
#>  6 Owen …  0.105   0.134  brown, gr… light      blue            52   male 
#>  7 Beru … -0.269  -0.132  brown      light      blue            47   fema…
#>  8 R5-D4  -2.22   -0.385  NA         white, red red             NA   none 
#>  9 Biggs…  0.249  -0.0786 black      light      brown           24   male 
#> 10 Obi-W…  0.220  -0.120  auburn, w… fair       blue-gray       57   male 
#> # ℹ 77 more rows
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>
starwars %>% mutate_at(c("height", "mass"), ~scale2(., na.rm = TRUE))
#> # A tibble: 87 × 14
#>    name    height    mass hair_color skin_color eye_color birth_year sex  
#>    <chr>    <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke … -0.0678 -0.120  blond      fair       blue            19   male 
#>  2 C-3PO  -0.212  -0.132  NA         gold       yellow         112   none 
#>  3 R2-D2  -2.25   -0.385  NA         white, bl… red             33   none 
#>  4 Darth…  0.795   0.228  none       white      yellow          41.9 male 
#>  5 Leia … -0.701  -0.285  brown      light      brown           19   fema…
#>  6 Owen …  0.105   0.134  brown, gr… light      blue            52   male 
#>  7 Beru … -0.269  -0.132  brown      light      blue            47   fema…
#>  8 R5-D4  -2.22   -0.385  NA         white, red red             NA   none 
#>  9 Biggs…  0.249  -0.0786 black      light      brown           24   male 
#> 10 Obi-W…  0.220  -0.120  auburn, w… fair       blue-gray       57   male 
#> # ℹ 77 more rows
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>
# ->
starwars %>% mutate(across(c("height", "mass"), ~ scale2(.x, na.rm = TRUE)))
#> # A tibble: 87 × 14
#>    name    height    mass hair_color skin_color eye_color birth_year sex  
#>    <chr>    <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke … -0.0678 -0.120  blond      fair       blue            19   male 
#>  2 C-3PO  -0.212  -0.132  NA         gold       yellow         112   none 
#>  3 R2-D2  -2.25   -0.385  NA         white, bl… red             33   none 
#>  4 Darth…  0.795   0.228  none       white      yellow          41.9 male 
#>  5 Leia … -0.701  -0.285  brown      light      brown           19   fema…
#>  6 Owen …  0.105   0.134  brown, gr… light      blue            52   male 
#>  7 Beru … -0.269  -0.132  brown      light      blue            47   fema…
#>  8 R5-D4  -2.22   -0.385  NA         white, red red             NA   none 
#>  9 Biggs…  0.249  -0.0786 black      light      brown           24   male 
#> 10 Obi-W…  0.220  -0.120  auburn, w… fair       blue-gray       57   male 
#> # ℹ 77 more rows
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>

# You can also supply selection helpers to _at() functions but you have
# to quote them with vars():
iris %>% mutate_at(vars(matches("Sepal")), log)
#> # A tibble: 150 × 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 
#> # ℹ 140 more rows
iris %>% mutate(across(matches("Sepal"), log))
#> # A tibble: 150 × 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 
#> # ℹ 140 more rows

# 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)
#> # A tibble: 87 × 14
#>    name    height    mass hair_color skin_color eye_color birth_year sex  
#>    <chr>    <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke … -0.0678 -0.120  blond      fair       blue          -0.443 male 
#>  2 C-3PO  -0.212  -0.132  NA         gold       yellow         0.158 none 
#>  3 R2-D2  -2.25   -0.385  NA         white, bl… red           -0.353 none 
#>  4 Darth…  0.795   0.228  none       white      yellow        -0.295 male 
#>  5 Leia … -0.701  -0.285  brown      light      brown         -0.443 fema…
#>  6 Owen …  0.105   0.134  brown, gr… light      blue          -0.230 male 
#>  7 Beru … -0.269  -0.132  brown      light      blue          -0.262 fema…
#>  8 R5-D4  -2.22   -0.385  NA         white, red red           NA     none 
#>  9 Biggs…  0.249  -0.0786 black      light      brown         -0.411 male 
#> 10 Obi-W…  0.220  -0.120  auburn, w… fair       blue-gray     -0.198 male 
#> # ℹ 77 more rows
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>
starwars %>% mutate(across(where(is.numeric), ~ scale2(.x, na.rm = TRUE)))
#> # A tibble: 87 × 14
#>    name    height    mass hair_color skin_color eye_color birth_year sex  
#>    <chr>    <dbl>   <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#>  1 Luke … -0.0678 -0.120  blond      fair       blue          -0.443 male 
#>  2 C-3PO  -0.212  -0.132  NA         gold       yellow         0.158 none 
#>  3 R2-D2  -2.25   -0.385  NA         white, bl… red           -0.353 none 
#>  4 Darth…  0.795   0.228  none       white      yellow        -0.295 male 
#>  5 Leia … -0.701  -0.285  brown      light      brown         -0.443 fema…
#>  6 Owen …  0.105   0.134  brown, gr… light      blue          -0.230 male 
#>  7 Beru … -0.269  -0.132  brown      light      blue          -0.262 fema…
#>  8 R5-D4  -2.22   -0.385  NA         white, red red           NA     none 
#>  9 Biggs…  0.249  -0.0786 black      light      brown         -0.411 male 
#> 10 Obi-W…  0.220  -0.120  auburn, w… fair       blue-gray     -0.198 male 
#> # ℹ 77 more rows
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>

# mutate_if() is particularly useful for transforming variables from
# one type to another
iris %>% mutate_if(is.factor, as.character)
#> # A tibble: 150 × 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 
#> # ℹ 140 more rows
iris %>% mutate_if(is.double, as.integer)
#> # A tibble: 150 × 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 
#> # ℹ 140 more rows
# ->
iris %>% mutate(across(where(is.factor), as.character))
#> # A tibble: 150 × 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 
#> # ℹ 140 more rows
iris %>% mutate(across(where(is.double), as.integer))
#> # A tibble: 150 × 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 
#> # ℹ 140 more rows

# 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))
#> # A tibble: 150 × 13
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  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 
#> # ℹ 140 more rows
#> # ℹ 8 more variables: Sepal.Length_fn1 <dbl>, 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>
iris %>% mutate_if(is.numeric, list(~scale2(.), ~log(.)))
#> # A tibble: 150 × 13
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  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 
#> # ℹ 140 more rows
#> # ℹ 8 more variables: Sepal.Length_scale2 <dbl>,
#> #   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>
iris %>% mutate_if(is.numeric, list(scale = scale2, log = log))
#> # A tibble: 150 × 13
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  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 
#> # ℹ 140 more rows
#> # ℹ 8 more variables: Sepal.Length_scale <dbl>, 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>
# ->
iris %>%
  as_tibble() %>%
  mutate(across(where(is.numeric), list(scale = scale2, log = log)))
#> # A tibble: 150 × 13
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  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 
#> # ℹ 140 more rows
#> # ℹ 8 more variables: Sepal.Length_scale <dbl>, 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>

# 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))
#> # A tibble: 150 × 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 
#> # ℹ 140 more rows
iris %>% mutate_if(is.numeric, list(scale = scale2))
#> # A tibble: 150 × 9
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  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 
#> # ℹ 140 more rows
#> # ℹ 4 more variables: Sepal.Length_scale <dbl>, Sepal.Width_scale <dbl>,
#> #   Petal.Length_scale <dbl>, Petal.Width_scale <dbl>
源代碼:R/colwise-mutate.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Mutate multiple columns。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。