# 平均値、分散を指定していい感じに作る
a_male <- round(rnorm(n = 160, mean = 60, sd = 10), 0)
a_female <- round(rnorm(n = 40, mean = 75, sd = 10), 0)
b_male <- round(rnorm(n = 40, mean = 55, sd = 10), 0)
b_female <- round(rnorm(n = 160, mean = 70, sd = 10), 0)
# その後、四捨五入しているので結局、微調整を手でしています。
a_male <- c(
45, 63, 55, 68, 60, 66, 64, 71, 46, 56,
71, 60, 42, 46, 53, 68, 72, 56, 64, 46,
47, 66, 65, 55, 55, 49, 66, 60, 44, 64,
60, 60, 34, 66, 63, 58, 62, 45, 66, 66,
60, 64, 53, 58, 69, 59, 65, 55, 58, 57,
76, 56, 67, 56, 76, 34, 53, 67, 49, 63,
59, 44, 53, 63, 48, 59, 60, 66, 53, 58,
40, 49, 56, 63, 57, 55, 74, 58, 61, 68,
74, 63, 64, 68, 77, 49, 69, 69, 63, 56,
67, 54, 49, 79, 60, 62, 63, 71, 71, 57,
56, 70, 81, 81, 58, 57, 54, 69, 64, 70,
56, 62, 66, 85, 63, 75, 59, 68, 65, 57,
59, 69, 91, 78, 59, 60, 55, 48, 52, 55,
52, 57, 70, 70, 58, 45, 49, 61, 46, 64,
50, 57, 71, 65, 47, 69, 59, 72, 49, 54,
71, 33, 61, 66, 53, 49, 47, 61, 57, 48)
a_female <- c(
80, 73, 92, 98, 75, 72, 65, 72, 62, 63,
83, 90, 82, 77, 79, 70, 73, 87, 72, 76,
70, 76, 82, 72, 67, 68, 71, 54, 78, 74,
90, 91, 74, 64, 63, 74, 77, 89, 63, 62)
b_male <- c(
56, 64, 61, 60, 53, 66, 65, 51, 50, 54,
50, 52, 51, 59, 59, 52, 61, 72, 42, 55,
42, 58, 49, 59, 60, 36, 75, 48, 34, 64,
53, 62, 46, 60, 47, 54, 59, 49, 55, 57)
b_female <- c(
90, 80, 62, 73, 60, 76, 75, 68, 82, 82,
67, 79, 71, 81, 54, 82, 65, 77, 71, 61,
62, 76, 71, 76, 62, 90, 55, 64, 83, 92,
66, 90, 71, 66, 80, 57, 63, 65, 71, 62,
55, 52, 68, 72, 79, 81, 76, 65, 65, 76,
75, 85, 74, 60, 59, 67, 63, 64, 81, 84,
81, 81, 55, 76, 80, 69, 84, 57, 78, 72,
63, 72, 82, 79, 58, 73, 52, 57, 58, 69,
73, 70, 83, 76, 58, 92, 65, 65, 65, 59,
80, 78, 68, 61, 64, 78, 62, 69, 81, 65,
91, 79, 68, 74, 68, 67, 81, 83, 67, 92,
67, 84, 75, 67, 67, 53, 62, 55, 68, 74,
65, 77, 75, 70, 68, 56, 74, 60, 65, 61,
54, 69, 68, 71, 67, 62, 66, 80, 69, 74,
69, 59, 69, 79, 69, 64, 67, 56, 66, 78,
85, 83, 53, 56, 56, 80, 53, 55, 70, 53)
# 表を作る関数
create_table <- \(male, female, school){
table_male <- tibble(male) |>
rename(得点 = male) |>
mutate(性別 = "男性")
table_female <- tibble(female) |>
rename(得点 = female) |>
mutate(性別 = "女性")
return(bind_rows(table_male, table_female) |>
mutate(学校 = school))
}
# A校とB校をユニオンする
data <- bind_rows(
create_table(a_male, a_female, "A校"),
create_table(b_male, b_female, "B校")
) |>
select(学校, 性別, 得点)
data %<>%
group_by(学校,性別) |>
mutate(平均点 = mean(得点))