使用パッケージ
library(tidyverse)
df <- as_tibble(iris) #説明に使うデータ
指定したカラムに関数を適用:mutate_at
df %>%
mutate_at(vars(starts_with("Sepal")), as.integer) %>%
mutate_at(vars(starts_with("Petal")), log, base = 2)
# A tibble: 150 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<int> <int> <dbl> <dbl> <fct>
1 5 3 0.485 -2.32 setosa
2 4 3 0.485 -2.32 setosa
3 4 3 0.379 -2.32 setosa
4 4 3 0.585 -2.32 setosa
5 5 3 0.485 -2.32 setosa
6 5 3 0.766 -1.32 setosa
7 4 3 0.485 -1.74 setosa
8 5 3 0.585 -2.32 setosa
9 4 2 0.485 -2.32 setosa
10 4 3 0.585 -3.32 setosa
# ... with 140 more rows
条件に合うカラムに関数を適用:mutate_if
df %>%
mutate_if(function(col)is.numeric(col), scale)
df %>%
mutate_if(~is.numeric(.x), scale)
# 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
複数の関数を適用する
funs()で複数の関数を渡す
df %>%
mutate_at(vars(starts_with("Sepal")), funs(scale,log))
df %>%
mutate_if(~is.numeric(.x), funs(scale,log))
# A tibble: 150 x 9
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_scale Sepal.Width_scale Sepal.Length_log Sepal.Width_log
<dbl> <dbl> <dbl> <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
1 5.1 3.5 1.4 0.2 setosa -0.898 1.02 1.63 1.25
2 4.9 3 1.4 0.2 setosa -1.14 -0.132 1.59 1.10
3 4.7 3.2 1.3 0.2 setosa -1.38 0.327 1.55 1.16
4 4.6 3.1 1.5 0.2 setosa -1.50 0.0979 1.53 1.13
5 5 3.6 1.4 0.2 setosa -1.02 1.25 1.61 1.28
6 5.4 3.9 1.7 0.4 setosa -0.535 1.93 1.69 1.36
7 4.6 3.4 1.4 0.3 setosa -1.50 0.786 1.53 1.22
8 5 3.4 1.5 0.2 setosa -1.02 0.786 1.61 1.22
9 4.4 2.9 1.4 0.2 setosa -1.74 -0.361 1.48 1.06
10 4.9 3.1 1.5 0.1 setosa -1.14 0.0979 1.59 1.13
# ... with 140 more rows
計算式を文字列で渡す:mutate_
df %>%
mutate_(.dots = set_names( "Sepal.Length / Sepal.Width", "new"))
# A tibble: 150 x 6
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
<dbl> <dbl> <dbl> <dbl> <fct> <dbl>
1 5.1 3.5 1.4 0.2 setosa 1.46
2 4.9 3 1.4 0.2 setosa 1.63
3 4.7 3.2 1.3 0.2 setosa 1.47
4 4.6 3.1 1.5 0.2 setosa 1.48
5 5 3.6 1.4 0.2 setosa 1.39
6 5.4 3.9 1.7 0.4 setosa 1.38
7 4.6 3.4 1.4 0.3 setosa 1.35
8 5 3.4 1.5 0.2 setosa 1.47
9 4.4 2.9 1.4 0.2 setosa 1.52
10 4.9 3.1 1.5 0.1 setosa 1.58
# ... with 140 more rows
計算式を文字列で渡す(応用編):mutate_
f_df <-
cross_df(list(
Sepal.Length = 5:4,
Sepal.Width = 3:4,
Petal.Length = 1.5,
Petal.Width = 0.2
)) %>%
mutate(f = str_c("(Sepal.Length >= ", Sepal.Length, ") & ",
"(Sepal.Width >= ", Sepal.Width, ") & ",
"(Petal.Length >= ", Petal.Length, ") & ",
"(Petal.Width >= ", Petal.Width, ")"))
list_f <- f_df %>% pull(f) %>% as.list
names(list_f) <- str_pad(1:length(list_f),width = 5,pad = "0") %>% str_c("arg_",.)
df %>%
mutate_(.dots = list_f) %>%
select(starts_with("arg_"))
# A tibble: 150 x 4
arg_00001 arg_00002 arg_00003 arg_00004
<lgl> <lgl> <lgl> <lgl>
1 FALSE FALSE FALSE FALSE
2 FALSE FALSE FALSE FALSE
3 FALSE FALSE FALSE FALSE
4 FALSE TRUE FALSE FALSE
5 FALSE FALSE FALSE FALSE
6 TRUE TRUE FALSE FALSE
7 FALSE FALSE FALSE FALSE
8 TRUE TRUE FALSE FALSE
9 FALSE FALSE FALSE FALSE
10 FALSE FALSE FALSE FALSE
# ... with 140 more rows