library(tidyverse)
library(janitor)2 パイプ演算子
ここでは複数の関数を用いた作業を内容を理解しやすくするためのパイプ演算子(Pipe Operator)について説明する.
|> あるいは %>% を使用する.後者についてはmagrittrパッケージに組み込まれているがtidyverseパッケージ(あるいはdplyr)やjanitor を呼び出せば使用できる.シンプルな関数(引数が1つであったり,第1引数を主に使う場合)であればどちらでも違いはないが,できるだけ|>を使用することを本資料では試みる.
mean(x) のような関数meanを使用する際, x(argument, 代入される値)を引数と呼ぶ.
2.1 ベクトルの例
a <- 1:100
a [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100
2.2 通常の関数の適用方法
mean(a)[1] 50.5
2.3 %>%を使う
以下のコードはすべてうまく実行される.
a %>% mean()
a %>% mean
a %>% mean(.)
a %>% mean(x = .)
a %>% {mean(.)}
a %>% {c(mean(.), sd(.), min(.), max(.), length(.))}
a %>% {list(mean(.), sd(.), min(.), max(.), length(.))}2.4 |> を使う
プレースホルダーを.から_に変更しただけだが, 以下のコードの多くでエラーが出る.native pipeは直感的な利用が難しい.
a |> mean()
a |> mean # エラー
a |> mean(_) # エラー
a |> mean(x = _)
a |> {mean(_)} # エラー
a |> {c(mean(_), sd(_), min(_), max(_), length(_))} # エラー
a |> {list(mean(_), sd(_), min(_), max(_), length(_))} # エラー2.5 新たな関数を作成する.
function(x) {} の代わりに \(x){}を使う.関数部分は()でくくり (\(x){})のようにする.慣れたら\(x)のあとの{}は省略する.またxを.としてもよい.
a |> mean()[1] 50.5
a |> mean(x = _)[1] 50.5
a |> (\(x) mean(x))()[1] 50.5
a |> (\(x) mean(x))(x = _)[1] 50.5
a |> (\(x) list(Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x), n = length(x)))()$Mean
[1] 50.5
$SD
[1] 29.01149
$Min
[1] 1
$Max
[1] 100
$n
[1] 100
a |> (\(x) list(Mean = mean(x)))()$Mean
[1] 50.5
a |> (\(x) c(Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x), n = length(x)))() Mean SD Min Max n
50.50000 29.01149 1.00000 100.00000 100.00000
a |> (\(x) list(Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x), n = length(x)))()$Mean
[1] 50.5
$SD
[1] 29.01149
$Min
[1] 1
$Max
[1] 100
$n
[1] 100
慣れるまでは読解が難しいが,次のように表現も可能である.
a |> mean()[1] 50.5
a |> (\(.) mean(.))()[1] 50.5
a |> (\(.) mean(.))(. = _)[1] 50.5
a |> (\(.) list(Mean = mean(.), SD = sd(.), Min = min(.), Max = max(.), n = length(.)))()$Mean
[1] 50.5
$SD
[1] 29.01149
$Min
[1] 1
$Max
[1] 100
$n
[1] 100
a |> (\(.) list(Mean = mean(.)))()$Mean
[1] 50.5
a |> (\(.) c(Mean = mean(.), SD = sd(.), Min = min(.), Max = max(.), n = length(.)))() Mean SD Min Max n
50.50000 29.01149 1.00000 100.00000 100.00000
a |> (\(.) list(Mean = mean(.), SD = sd(.), Min = min(.), Max = max(.), n = length(.)))()$Mean
[1] 50.5
$SD
[1] 29.01149
$Min
[1] 1
$Max
[1] 100
$n
[1] 100
2.6 第2引数がある場合
y <- c(1, NA, 3:100)
y [1] 1 NA 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100
y %>% mean[1] NA
y %>% mean(na.rm = TRUE)[1] 50.9899
y |> mean()[1] NA
y |> mean(na.rm = TRUE)[1] 50.9899
引数が1つしかない場合は x |> f や x %>% f は f(x)を行っているのと同じ. 引数が複数ある場合は1つ目の引数(第1引数)にxが用いられる. 引数が複数ある場合に1つ目の引数以外にxを適用したい場合は |>であれば_を使用する.%>%であれば. を使用する. y |> f(x, _) とすれば f(x, y) を実行しているのと同じである.
pi[1] 3.141593
3 |> round(pi, digits = _)[1] 3.142
3 %>% round(pi, digits = .)[1] 3.142
2.7 スターウォーズデータの例
starwars# A tibble: 87 × 14
name height mass hair_color skin_color eye_color birth_year sex gender
<chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
1 Luke Sk… 172 77 blond fair blue 19 male mascu…
2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
4 Darth V… 202 136 none white yellow 41.9 male mascu…
5 Leia Or… 150 49 brown light brown 19 fema… femin…
6 Owen La… 178 120 brown, gr… light blue 52 male mascu…
7 Beru Wh… 165 75 brown light blue 47 fema… femin…
8 R5-D4 97 32 <NA> white, red red NA none mascu…
9 Biggs D… 183 84 black light brown 24 male mascu…
10 Obi-Wan… 182 77 auburn, w… fair blue-gray 57 male mascu…
# ℹ 77 more rows
# ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
# vehicles <list>, starships <list>
# 中身を確認
glimpse(starwars)Rows: 87
Columns: 14
$ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or…
$ height <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, 2…
$ mass <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77.…
$ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", N…
$ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light", "…
$ eye_color <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue",…
$ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0, …
$ sex <chr> "male", "none", "none", "male", "female", "male", "female",…
$ gender <chr> "masculine", "masculine", "masculine", "masculine", "femini…
$ homeworld <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "T…
$ species <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Huma…
$ films <list> <"The Empire Strikes Back", "Revenge of the Sith", "Return…
$ vehicles <list> <"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "Imp…
$ starships <list> <"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1",…
starwars %>% glimpse()Rows: 87
Columns: 14
$ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or…
$ height <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, 2…
$ mass <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77.…
$ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", N…
$ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light", "…
$ eye_color <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue",…
$ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0, …
$ sex <chr> "male", "none", "none", "male", "female", "male", "female",…
$ gender <chr> "masculine", "masculine", "masculine", "masculine", "femini…
$ homeworld <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "T…
$ species <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Huma…
$ films <list> <"The Empire Strikes Back", "Revenge of the Sith", "Return…
$ vehicles <list> <"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "Imp…
$ starships <list> <"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1",…
starwars |> glimpse()Rows: 87
Columns: 14
$ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or…
$ height <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, 2…
$ mass <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77.…
$ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", N…
$ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light", "…
$ eye_color <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue",…
$ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0, …
$ sex <chr> "male", "none", "none", "male", "female", "male", "female",…
$ gender <chr> "masculine", "masculine", "masculine", "masculine", "femini…
$ homeworld <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "T…
$ species <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Huma…
$ films <list> <"The Empire Strikes Back", "Revenge of the Sith", "Return…
$ vehicles <list> <"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "Imp…
$ starships <list> <"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1",…
# 変数名
names(starwars) [1] "name" "height" "mass" "hair_color" "skin_color"
[6] "eye_color" "birth_year" "sex" "gender" "homeworld"
[11] "species" "films" "vehicles" "starships"
starwars %>% names() [1] "name" "height" "mass" "hair_color" "skin_color"
[6] "eye_color" "birth_year" "sex" "gender" "homeworld"
[11] "species" "films" "vehicles" "starships"
starwars |> names() [1] "name" "height" "mass" "hair_color" "skin_color"
[6] "eye_color" "birth_year" "sex" "gender" "homeworld"
[11] "species" "films" "vehicles" "starships"
# 平均
starwars$height [1] 172 167 96 202 150 178 165 97 183 182 188 180 228 180 173 175 170 180 66
[20] 170 183 200 190 177 175 180 150 NA 88 160 193 191 170 196 224 206 183 137
[39] 112 183 163 175 180 178 94 122 163 188 198 196 171 184 188 264 188 196 185
[58] 157 183 183 170 166 165 193 191 183 168 198 229 213 167 79 96 193 191 178
[77] 216 234 188 178 206 NA NA NA NA NA 165
mean(starwars$height)[1] NA
mean(starwars$height, na.rm = TRUE)[1] 174.358
starwars$height %>% mean(na.rm = TRUE)[1] 174.358
starwars$height |> mean(na.rm = TRUE)[1] 174.358
starwars |> pull(height) |> mean(na.rm = TRUE)[1] 174.358
mean(starwars$mass)[1] NA
mean(starwars$mass, na.rm = TRUE)[1] 97.31186
starwars$mass %>% mean(na.rm = TRUE)[1] 97.31186
starwars$mass |> mean(na.rm = TRUE)[1] 97.31186
starwars |> pull(mass) |> mean(na.rm = TRUE)[1] 97.31186
# 度数分布
# 度数だけならcount
count(starwars, gender)# A tibble: 3 × 2
gender n
<chr> <int>
1 feminine 17
2 masculine 66
3 <NA> 4
count(starwars, sex)# A tibble: 5 × 2
sex n
<chr> <int>
1 female 16
2 hermaphroditic 1
3 male 60
4 none 6
5 <NA> 4
starwars %>% count(gender)# A tibble: 3 × 2
gender n
<chr> <int>
1 feminine 17
2 masculine 66
3 <NA> 4
starwars %>% count(sex)# A tibble: 5 × 2
sex n
<chr> <int>
1 female 16
2 hermaphroditic 1
3 male 60
4 none 6
5 <NA> 4
starwars |> count(gender)# A tibble: 3 × 2
gender n
<chr> <int>
1 feminine 17
2 masculine 66
3 <NA> 4
starwars |> count(sex)# A tibble: 5 × 2
sex n
<chr> <int>
1 female 16
2 hermaphroditic 1
3 male 60
4 none 6
5 <NA> 4
# 変数の組み合わせごとに度数を表示
count(starwars, sex, gender)# A tibble: 6 × 3
sex gender n
<chr> <chr> <int>
1 female feminine 16
2 hermaphroditic masculine 1
3 male masculine 60
4 none feminine 1
5 none masculine 5
6 <NA> <NA> 4
starwars %>% count(sex, gender)# A tibble: 6 × 3
sex gender n
<chr> <chr> <int>
1 female feminine 16
2 hermaphroditic masculine 1
3 male masculine 60
4 none feminine 1
5 none masculine 5
6 <NA> <NA> 4
starwars |> count(sex, gender)# A tibble: 6 × 3
sex gender n
<chr> <chr> <int>
1 female feminine 16
2 hermaphroditic masculine 1
3 male masculine 60
4 none feminine 1
5 none masculine 5
6 <NA> <NA> 4
# %も表示したいならtabyl
tabyl(starwars, gender) gender n percent valid_percent
feminine 17 0.19540230 0.2048193
masculine 66 0.75862069 0.7951807
<NA> 4 0.04597701 NA
tabyl(starwars, sex) sex n percent valid_percent
female 16 0.18390805 0.19277108
hermaphroditic 1 0.01149425 0.01204819
male 60 0.68965517 0.72289157
none 6 0.06896552 0.07228916
<NA> 4 0.04597701 NA
starwars %>% tabyl(gender) gender n percent valid_percent
feminine 17 0.19540230 0.2048193
masculine 66 0.75862069 0.7951807
<NA> 4 0.04597701 NA
starwars %>% tabyl(sex) sex n percent valid_percent
female 16 0.18390805 0.19277108
hermaphroditic 1 0.01149425 0.01204819
male 60 0.68965517 0.72289157
none 6 0.06896552 0.07228916
<NA> 4 0.04597701 NA
starwars |> tabyl(gender) gender n percent valid_percent
feminine 17 0.19540230 0.2048193
masculine 66 0.75862069 0.7951807
<NA> 4 0.04597701 NA
starwars |> tabyl(sex) sex n percent valid_percent
female 16 0.18390805 0.19277108
hermaphroditic 1 0.01149425 0.01204819
male 60 0.68965517 0.72289157
none 6 0.06896552 0.07228916
<NA> 4 0.04597701 NA
# 変数の選択
starwars# A tibble: 87 × 14
name height mass hair_color skin_color eye_color birth_year sex gender
<chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
1 Luke Sk… 172 77 blond fair blue 19 male mascu…
2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
4 Darth V… 202 136 none white yellow 41.9 male mascu…
5 Leia Or… 150 49 brown light brown 19 fema… femin…
6 Owen La… 178 120 brown, gr… light blue 52 male mascu…
7 Beru Wh… 165 75 brown light blue 47 fema… femin…
8 R5-D4 97 32 <NA> white, red red NA none mascu…
9 Biggs D… 183 84 black light brown 24 male mascu…
10 Obi-Wan… 182 77 auburn, w… fair blue-gray 57 male mascu…
# ℹ 77 more rows
# ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
# vehicles <list>, starships <list>
dplyr::select(starwars, name, height, mass)# A tibble: 87 × 3
name height mass
<chr> <int> <dbl>
1 Luke Skywalker 172 77
2 C-3PO 167 75
3 R2-D2 96 32
4 Darth Vader 202 136
5 Leia Organa 150 49
6 Owen Lars 178 120
7 Beru Whitesun lars 165 75
8 R5-D4 97 32
9 Biggs Darklighter 183 84
10 Obi-Wan Kenobi 182 77
# ℹ 77 more rows
starwars %>% dplyr::select(name, height, mass)# A tibble: 87 × 3
name height mass
<chr> <int> <dbl>
1 Luke Skywalker 172 77
2 C-3PO 167 75
3 R2-D2 96 32
4 Darth Vader 202 136
5 Leia Organa 150 49
6 Owen Lars 178 120
7 Beru Whitesun lars 165 75
8 R5-D4 97 32
9 Biggs Darklighter 183 84
10 Obi-Wan Kenobi 182 77
# ℹ 77 more rows
starwars |> dplyr::select(name, height, mass)# A tibble: 87 × 3
name height mass
<chr> <int> <dbl>
1 Luke Skywalker 172 77
2 C-3PO 167 75
3 R2-D2 96 32
4 Darth Vader 202 136
5 Leia Organa 150 49
6 Owen Lars 178 120
7 Beru Whitesun lars 165 75
8 R5-D4 97 32
9 Biggs Darklighter 183 84
10 Obi-Wan Kenobi 182 77
# ℹ 77 more rows
# 回帰分析
lm(height ~ sex, data = starwars)
Call:
lm(formula = height ~ sex, data = starwars)
Coefficients:
(Intercept) sexhermaphroditic sexmale sexnone
169.267 5.733 9.839 -38.067
summary(lm(height ~ sex, data = starwars))
Call:
lm(formula = height ~ sex, data = starwars)
Residuals:
Min 1Q Median 3Q Max
-113.105 -6.896 1.895 13.895 84.895
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 169.267 8.780 19.279 <2e-16 ***
sexhermaphroditic 5.733 35.120 0.163 0.8708
sexmale 9.839 9.868 0.997 0.3220
sexnone -38.067 17.560 -2.168 0.0334 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 34 on 74 degrees of freedom
(9 observations deleted due to missingness)
Multiple R-squared: 0.1137, Adjusted R-squared: 0.07781
F-statistic: 3.166 on 3 and 74 DF, p-value: 0.02937
starwars %>% lm(height ~ sex, data = .)
Call:
lm(formula = height ~ sex, data = .)
Coefficients:
(Intercept) sexhermaphroditic sexmale sexnone
169.267 5.733 9.839 -38.067
starwars %>% lm(height ~ sex, data = .) %>% summary()
Call:
lm(formula = height ~ sex, data = .)
Residuals:
Min 1Q Median 3Q Max
-113.105 -6.896 1.895 13.895 84.895
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 169.267 8.780 19.279 <2e-16 ***
sexhermaphroditic 5.733 35.120 0.163 0.8708
sexmale 9.839 9.868 0.997 0.3220
sexnone -38.067 17.560 -2.168 0.0334 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 34 on 74 degrees of freedom
(9 observations deleted due to missingness)
Multiple R-squared: 0.1137, Adjusted R-squared: 0.07781
F-statistic: 3.166 on 3 and 74 DF, p-value: 0.02937
starwars %>%
drop_na(height, sex) %>%
lm(height ~ sex, data = .) %>%
summary()
Call:
lm(formula = height ~ sex, data = .)
Residuals:
Min 1Q Median 3Q Max
-113.105 -6.896 1.895 13.895 84.895
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 169.267 8.780 19.279 <2e-16 ***
sexhermaphroditic 5.733 35.120 0.163 0.8708
sexmale 9.839 9.868 0.997 0.3220
sexnone -38.067 17.560 -2.168 0.0334 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 34 on 74 degrees of freedom
Multiple R-squared: 0.1137, Adjusted R-squared: 0.07781
F-statistic: 3.166 on 3 and 74 DF, p-value: 0.02937
## placeholderが . ではなく _ になっていることに注意
starwars |> lm(height ~ sex, data = _)
Call:
lm(formula = height ~ sex, data = starwars)
Coefficients:
(Intercept) sexhermaphroditic sexmale sexnone
169.267 5.733 9.839 -38.067
starwars |> lm(height ~ sex, data = _) %>% summary()
Call:
lm(formula = height ~ sex, data = starwars)
Residuals:
Min 1Q Median 3Q Max
-113.105 -6.896 1.895 13.895 84.895
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 169.267 8.780 19.279 <2e-16 ***
sexhermaphroditic 5.733 35.120 0.163 0.8708
sexmale 9.839 9.868 0.997 0.3220
sexnone -38.067 17.560 -2.168 0.0334 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 34 on 74 degrees of freedom
(9 observations deleted due to missingness)
Multiple R-squared: 0.1137, Adjusted R-squared: 0.07781
F-statistic: 3.166 on 3 and 74 DF, p-value: 0.02937
starwars |>
drop_na(height, sex) |>
lm(height ~ sex, data = _) |>
summary()
Call:
lm(formula = height ~ sex, data = drop_na(starwars, height, sex))
Residuals:
Min 1Q Median 3Q Max
-113.105 -6.896 1.895 13.895 84.895
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 169.267 8.780 19.279 <2e-16 ***
sexhermaphroditic 5.733 35.120 0.163 0.8708
sexmale 9.839 9.868 0.997 0.3220
sexnone -38.067 17.560 -2.168 0.0334 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 34 on 74 degrees of freedom
Multiple R-squared: 0.1137, Adjusted R-squared: 0.07781
F-statistic: 3.166 on 3 and 74 DF, p-value: 0.02937
# データそのものではなくそのデータの変数を使用する場合
starwars$height %>% mean(na.rm = TRUE)[1] 174.358
starwars %>% .$height %>% mean(na.rm = TRUE)[1] 174.358
starwars$height |> mean(na.rm = TRUE)[1] 174.358
starwars |> _$height |> mean(na.rm = TRUE)[1] 174.358
starwars |> pull(height) |> mean(na.rm = TRUE)[1] 174.358
# 応用
starwars %>%
filter(sex == "male") %>%
summarise(Mean = mean(height, na.rm = TRUE),
SD = sd(height, na.rm = TRUE),
n = sum(!is.na(height)),
ll = Mean + qt(0.025, n-1) * SD / sqrt(n),
ul = Mean + qt(0.975, n-1) * SD / sqrt(n)
)# A tibble: 1 × 5
Mean SD n ll ul
<dbl> <dbl> <int> <dbl> <dbl>
1 179. 36.0 57 170. 189.
starwars %>%
filter(sex == "female") %>%
summarise(Mean = mean(height, na.rm = TRUE),
SD = sd(height, na.rm = TRUE),
n = sum(!is.na(height)),
ll = Mean + qt(0.025, n-1) * SD / sqrt(n),
ul = Mean + qt(0.975, n-1) * SD / sqrt(n)
)# A tibble: 1 × 5
Mean SD n ll ul
<dbl> <dbl> <int> <dbl> <dbl>
1 169. 15.3 15 161. 178.
starwars |>
filter(sex == "female") |>
summarise(Mean = mean(height, na.rm = TRUE),
SD = sd(height, na.rm = TRUE),
n = sum(!is.na(height)),
ll = Mean + qt(0.025, n-1) * SD / sqrt(n),
ul = Mean + qt(0.975, n-1) * SD / sqrt(n)
)# A tibble: 1 × 5
Mean SD n ll ul
<dbl> <dbl> <int> <dbl> <dbl>
1 169. 15.3 15 161. 178.
# 2つに適用
1:100 %>% mean()[1] 50.5
1:100 %>% mean[1] 50.5
1:100 %>% {c(mean(.), sd(.))}[1] 50.50000 29.01149
1:100 %>% {list(mean(.), sd(.))}[[1]]
[1] 50.5
[[2]]
[1] 29.01149
starwars %>% .$height %>% {c(mean(.,na.rm = TRUE), sd(.,na.rm = TRUE))}[1] 174.35802 34.77043
1:100 |> mean()[1] 50.5
1:100 |> (\(x) c(mean(x), sd(x)))()[1] 50.50000 29.01149
1:100 |> (\(x) list(mean(x), sd(x)))()[[1]]
[1] 50.5
[[2]]
[1] 29.01149
starwars |> _$height |> (\(x)(c(mean(x,na.rm = TRUE), sd(x,na.rm = TRUE))))()[1] 174.35802 34.77043
quote(starwars |> _$height |> (\(x)(c(mean(x,na.rm = TRUE), sd(x,na.rm = TRUE))))())(function(x) (c(mean(x, na.rm = TRUE), sd(x, na.rm = TRUE))))(starwars$height)