Help us understand the problem. What is going on with this article?

【メモ】dplyrのscoped functionを使いこなす(_all/_if/_at)

はじめに

dplyrを使いはじめてselectやmutate関数の後につく「_all」とか「_if」とか「_at」って何なんだろうって思っていたのですが、ようやく理解が出来てきた気がします。
Rのぽんこつ初心者で恐縮ですが、わかったことをメモしておこうかと思います。

dplyrのscoped functionについて

dplyrを使うRプログラマーならだれでもfilter()関数だったりsummarise()関数やrename()関数などに触れていると思うのですが、それぞれ「_all」、「_if」、「_at」といった具合にサフィックスが付くことがあります。
これにはそれぞれ以下のような意味があります。

サフィックス 意味 引数
_all 全ての列に対して同じ操作を適用する データ,適用する関数,...
_if 条件に合致する列に対して操作を適用する データ,条件,適用する関数,...
_at 指定した名前の列に対して操作を適用する データ,対象の列,適用する関数,...

では、さっそく例を挙げながら使ってみます。

_all()

all()関数は全ての列に対して同じ操作を適用する関数です。

scoped_function.r

#全ての列名に対して小文字⇒大文字に変換する
mtcars %>>%
  rename_all( toupper) 

mtcars %>>%
  select_all(toupper)

#全ての列に対し0.4を掛ける
mtcars %>%
  mutate_all(funs(. * 0.4))

実行結果は以下になります。

> #全ての列名に対して小文字⇒大文字に変換する
> mtcars %>>%
+   rename_all( toupper) 
                     MPG CYL  DISP  HP DRAT    WT  QSEC VS AM GEAR CARB
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> 
> mtcars %>>%
+   select_all(toupper)
                     MPG CYL  DISP  HP DRAT    WT  QSEC VS AM GEAR CARB
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> 
> #全ての列に対し0.4を掛ける
> mtcars %>%
+   mutate_all(funs(. * 0.4))
     mpg cyl   disp    hp  drat     wt  qsec  vs  am gear carb
1   8.40 2.4  64.00  44.0 1.560 1.0480 6.584 0.0 0.4  1.6  1.6
2   8.40 2.4  64.00  44.0 1.560 1.1500 6.808 0.0 0.4  1.6  1.6
3   9.12 1.6  43.20  37.2 1.540 0.9280 7.444 0.4 0.4  1.6  0.4
4   8.56 2.4 103.20  44.0 1.232 1.2860 7.776 0.4 0.0  1.2  0.4
5   7.48 3.2 144.00  70.0 1.260 1.3760 6.808 0.0 0.0  1.2  0.8
6   7.24 2.4  90.00  42.0 1.104 1.3840 8.088 0.4 0.0  1.2  0.4
7   5.72 3.2 144.00  98.0 1.284 1.4280 6.336 0.0 0.0  1.2  1.6
8   9.76 1.6  58.68  24.8 1.476 1.2760 8.000 0.4 0.0  1.6  0.8
9   9.12 1.6  56.32  38.0 1.568 1.2600 9.160 0.4 0.0  1.6  0.8
10  7.68 2.4  67.04  49.2 1.568 1.3760 7.320 0.4 0.0  1.6  1.6
11  7.12 2.4  67.04  49.2 1.568 1.3760 7.560 0.4 0.0  1.6  1.6
12  6.56 3.2 110.32  72.0 1.228 1.6280 6.960 0.0 0.0  1.2  1.2
13  6.92 3.2 110.32  72.0 1.228 1.4920 7.040 0.0 0.0  1.2  1.2
14  6.08 3.2 110.32  72.0 1.228 1.5120 7.200 0.0 0.0  1.2  1.2
15  4.16 3.2 188.80  82.0 1.172 2.1000 7.192 0.0 0.0  1.2  1.6
16  4.16 3.2 184.00  86.0 1.200 2.1696 7.128 0.0 0.0  1.2  1.6
17  5.88 3.2 176.00  92.0 1.292 2.1380 6.968 0.0 0.0  1.2  1.6
18 12.96 1.6  31.48  26.4 1.632 0.8800 7.788 0.4 0.4  1.6  0.4
19 12.16 1.6  30.28  20.8 1.972 0.6460 7.408 0.4 0.4  1.6  0.8
20 13.56 1.6  28.44  26.0 1.688 0.7340 7.960 0.4 0.4  1.6  0.4
21  8.60 1.6  48.04  38.8 1.480 0.9860 8.004 0.4 0.0  1.2  0.4
22  6.20 3.2 127.20  60.0 1.104 1.4080 6.748 0.0 0.0  1.2  0.8
23  6.08 3.2 121.60  60.0 1.260 1.3740 6.920 0.0 0.0  1.2  0.8
24  5.32 3.2 140.00  98.0 1.492 1.5360 6.164 0.0 0.0  1.2  1.6
25  7.68 3.2 160.00  70.0 1.232 1.5380 6.820 0.0 0.0  1.2  0.8
26 10.92 1.6  31.60  26.4 1.632 0.7740 7.560 0.4 0.4  1.6  0.4
27 10.40 1.6  48.12  36.4 1.772 0.8560 6.680 0.0 0.4  2.0  0.8
28 12.16 1.6  38.04  45.2 1.508 0.6052 6.760 0.4 0.4  2.0  0.8
29  6.32 3.2 140.40 105.6 1.688 1.2680 5.800 0.0 0.4  2.0  1.6
30  7.88 2.4  58.00  70.0 1.448 1.1080 6.200 0.0 0.4  2.0  2.4
31  6.00 3.2 120.40 134.0 1.416 1.4280 5.840 0.0 0.4  2.0  3.2
32  8.56 1.6  48.40  43.6 1.644 1.1120 7.440 0.4 0.4  1.6  0.8

_if()

_if()関数は操作する列を、指定した条件を満たすものだけに絞り込むことができる関数です。

scoped_function.r

#与えられた要素がすべて整数の場合TRUEを返す
is_whole <- function(x) all(floor(x) == x)  

#整数の列に対して列名を小文字⇒大文字に変換する
mtcars %>>%
  rename_if( is_whole,toupper)  


#整数の列だけ取り出す
mtcars %>>%
  select_if(is_whole) 

#列の型がnumericだった場合、小数点以下を丸める
mpg %>>%
  mutate_if(is.numeric,round)

実行結果は以下の通りです。

> mtcars %>>%
+   rename_if( is_whole,toupper)  
                     mpg CYL  disp  HP drat    wt  qsec VS AM GEAR CARB
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> 
> 
> #整数の列だけ取り出す
> mtcars %>>%
+   select_if(is_whole) 
                    cyl  hp vs am gear carb
Mazda RX4             6 110  0  1    4    4
Mazda RX4 Wag         6 110  0  1    4    4
Datsun 710            4  93  1  1    4    1
Hornet 4 Drive        6 110  1  0    3    1
Hornet Sportabout     8 175  0  0    3    2
Valiant               6 105  1  0    3    1
Duster 360            8 245  0  0    3    4
Merc 240D             4  62  1  0    4    2
Merc 230              4  95  1  0    4    2
Merc 280              6 123  1  0    4    4
Merc 280C             6 123  1  0    4    4
Merc 450SE            8 180  0  0    3    3
Merc 450SL            8 180  0  0    3    3
Merc 450SLC           8 180  0  0    3    3
Cadillac Fleetwood    8 205  0  0    3    4
Lincoln Continental   8 215  0  0    3    4
Chrysler Imperial     8 230  0  0    3    4
Fiat 128              4  66  1  1    4    1
Honda Civic           4  52  1  1    4    2
Toyota Corolla        4  65  1  1    4    1
Toyota Corona         4  97  1  0    3    1
Dodge Challenger      8 150  0  0    3    2
AMC Javelin           8 150  0  0    3    2
Camaro Z28            8 245  0  0    3    4
Pontiac Firebird      8 175  0  0    3    2
Fiat X1-9             4  66  1  1    4    1
Porsche 914-2         4  91  0  1    5    2
Lotus Europa          4 113  1  1    5    2
Ford Pantera L        8 264  0  1    5    4
Ferrari Dino          6 175  0  1    5    6
Maserati Bora         8 335  0  1    5    8
Volvo 142E            4 109  1  1    4    2
> 
> #numericの型の列に対して、小数点以下を丸める
> mpg %>>%
+   mutate_if(is.numeric,round)
# A tibble: 234 x 11
   manufacturer model   displ  year   cyl trans   drv     cty   hwy fl    class
   <chr>        <chr>   <dbl> <dbl> <dbl> <chr>   <chr> <dbl> <dbl> <chr> <chr>
 1 audi         a4          2  1999     4 auto(l~ f        18    29 p     comp~
 2 audi         a4          2  1999     4 manual~ f        21    29 p     comp~
 3 audi         a4          2  2008     4 manual~ f        20    31 p     comp~
 4 audi         a4          2  2008     4 auto(a~ f        21    30 p     comp~
 5 audi         a4          3  1999     6 auto(l~ f        16    26 p     comp~
 6 audi         a4          3  1999     6 manual~ f        18    26 p     comp~
 7 audi         a4          3  2008     6 auto(a~ f        18    27 p     comp~
 8 audi         a4 qua~     2  1999     4 manual~ 4        18    26 p     comp~
 9 audi         a4 qua~     2  1999     4 auto(l~ 4        16    25 p     comp~
10 audi         a4 qua~     2  2008     4 manual~ 4        20    28 p     comp~
# ... with 224 more rows

_at()

この関数は指定した名前の列にだけ操作を適用します。
また、vars()という関数でラップすることで starts_with("〇〇〇")-(〇:〇)といった記述方法がselectのセマンティクスで解釈されます。

scoped_function.r

#1~3列目を除いて列名を小文字⇒大文字に変換
mtcars %>>%
  rename_at(vars(-(1:3)), toupper)

#arを含まない列に対して列名を小文字⇒大文字に変換
mtcars %>>%
  select_at( vars(-contains("ar")), toupper)

#指定した列を標準化
mtcars %>>%
  mutate_at(c("mpg","disp","drat","wt","qsec"),scale)

実行結果は以下になります。

> mtcars %>>%
+   rename_at(c("vs","carb"),toupper) #vsとcarbに対して列名を小文字⇒大文字に変換
                     mpg cyl  disp  hp drat    wt  qsec VS am gear CARB
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> #1~3列目を除いて列名を小文字⇒大文字に変換
> mtcars %>>%
+   rename_at(vars(-(1:3)), toupper)
                     mpg cyl  disp  HP DRAT    WT  QSEC VS AM GEAR CARB
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> 
> #arを含まない列に対して列名を小文字⇒大文字に変換
> mtcars %>>%
+   select_at( vars(-contains("ar")), toupper)
                     MPG CYL  DISP  HP DRAT    WT  QSEC VS AM
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1
> 
> #指定した列を標準化
> mtcars %>>%
+   mutate_at(c("mpg","disp","drat","wt","qsec"),scale)
           mpg cyl        disp  hp        drat           wt        qsec vs am
1   0.15088482   6 -0.57061982 110  0.56751369 -0.610399567 -0.77716515  0  1
2   0.15088482   6 -0.57061982 110  0.56751369 -0.349785269 -0.46378082  0  1
3   0.44954345   4 -0.99018209  93  0.47399959 -0.917004624  0.42600682  1  1
4   0.21725341   6  0.22009369 110 -0.96611753 -0.002299538  0.89048716  1  0
5  -0.23073453   8  1.04308123 175 -0.83519779  0.227654255 -0.46378082  0  0
6  -0.33028740   6 -0.04616698 105 -1.56460776  0.248094592  1.32698675  1  0
7  -0.96078893   8  1.04308123 245 -0.72298087  0.360516446 -1.12412636  0  0
8   0.71501778   4 -0.67793094  62  0.17475447 -0.027849959  1.20387148  1  0
9   0.44954345   4 -0.72553512  95  0.60491932 -0.068730634  2.82675459  1  0
10 -0.14777380   6 -0.50929918 123  0.60491932  0.227654255  0.25252621  1  0
11 -0.38006384   6 -0.50929918 123  0.60491932  0.227654255  0.58829513  1  0
12 -0.61235388   8  0.36371309 180 -0.98482035  0.871524874 -0.25112717  0  0
13 -0.46302456   8  0.36371309 180 -0.98482035  0.524039143 -0.13920420  0  0
14 -0.81145962   8  0.36371309 180 -0.98482035  0.575139986  0.08464175  0  0
15 -1.60788262   8  1.94675381 205 -1.24665983  2.077504765  0.07344945  0  0
16 -1.60788262   8  1.84993175 215 -1.11574009  2.255335698 -0.01608893  0  0
17 -0.89442035   8  1.68856165 230 -0.68557523  2.174596366 -0.23993487  0  0
18  2.04238943   4 -1.22658929  66  0.90416444 -1.039646647  0.90727560  1  1
19  1.71054652   4 -1.25079481  52  2.49390411 -1.637526508  0.37564148  1  1
20  2.29127162   4 -1.28790993  65  1.16600392 -1.412682800  1.14790999  1  1
21  0.23384555   4 -0.89255318  97  0.19345729 -0.768812180  1.20946763  1  0
22 -0.76168319   8  0.70420401 150 -1.56460776  0.309415603 -0.54772305  0  0
23 -0.81145962   8  0.59124494 150 -0.83519779  0.222544170 -0.30708866  0  0
24 -1.12671039   8  0.96239618 245  0.24956575  0.636460997 -1.36476075  0  0
25 -0.14777380   8  1.36582144 175 -0.96611753  0.641571082 -0.44699237  0  0
26  1.19619000   4 -1.22416874  66  0.90416444 -1.310481114  0.58829513  1  1
27  0.98049211   4 -0.89093948  91  1.55876313 -1.100967659 -0.64285758  0  1
28  1.71054652   4 -1.09426581 113  0.32437703 -1.741772228 -0.53093460  1  1
29 -0.71190675   8  0.97046468 264  1.16600392 -0.048290296 -1.87401028  0  1
30 -0.06481307   6 -0.69164740 175  0.04383473 -0.457097039 -1.31439542  0  1
31 -0.84464392   8  0.56703942 335 -0.10578782  0.360516446 -1.81804880  0  1
32  0.21725341   4 -0.88529152 109  0.96027290 -0.446876870  0.42041067  1  1
   gear carb
1     4    4
2     4    4
3     4    1
4     3    1
5     3    2
6     3    1
7     3    4
8     4    2
9     4    2
10    4    4
11    4    4
12    3    3
13    3    3
14    3    3
15    3    4
16    3    4
17    3    4
18    4    1
19    4    2
20    4    1
21    3    1
22    3    2
23    3    2
24    3    4
25    3    2
26    4    1
27    5    2
28    5    2
29    5    4
30    5    6
31    5    8
32    4    2

おわりに

ようやく_all/_if/_at関数の謎が解けて良かったです。
より精緻なデータ弄りができそうな予感です。
引き続き、やっていきましょう。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away