Участник:Vakhrushevavs521/R lesson: различия между версиями
Материал из Поле цифровой дидактики
Patarakin (обсуждение | вклад) Новая страница: «Здесь будет пример кода на языке R» |
Patarakin (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
Здесь будет пример кода на языке R | Здесь будет пример кода на языке R | ||
<syntaxhighlight lang="R" line> | |||
getwd() | |||
#------------ | |||
library(learningtower); | |||
library(dplyr) | |||
library(ggplot2) | |||
#---- | |||
student_data_2015 <- load_student(2015) | |||
student_data_2012 <- load_student(2012) | |||
str(student_data_2015) | |||
glimpse(student_data_2015) | |||
summary(student_data_2015) | |||
glimpse(student_data_2015) | |||
data_proc <- student_data_2015 %>% | |||
filter(country %in% c("RUS", "USA", "SGP")) %>% | |||
group_by(country, gender) %>% | |||
summarise( | |||
mean_math = weighted.mean(math, stu_wgt, na.rm = TRUE), | |||
mean_science = weighted.mean(science, stu_wgt, na.rm = TRUE), | |||
n = n(), | |||
.groups = "drop" | |||
) %>% | |||
na.omit() | |||
print(data_proc) | |||
ggplot(student_data_2015 %>% filter(country %in% c("RUS", "USA", "SGP")), | |||
aes(x = country, y = math, fill = gender)) + | |||
geom_boxplot(alpha = 0.7) + | |||
labs(title = "Распределение баллов по математике (PISA 2015)", | |||
x = "Страна", y = "Баллы по математике", fill = "Пол") + | |||
theme_minimal() + | |||
theme(legend.position = "bottom") | |||
#---- | |||
ggplot(student_data_2012 %>% filter(country %in% c("RUS", "USA", "SGP")), | |||
aes(x = country, y = math, fill = gender)) + | |||
geom_boxplot(alpha = 0.7) + | |||
labs(title = "Распределение баллов по математике (PISA 2012)", | |||
x = "Страна", y = "Баллы по математике", fill = "Пол") + | |||
theme_minimal() + | |||
theme(legend.position = "bottom") | |||
##--- | |||
data_long <- data_proc %>% | |||
tidyr::pivot_longer(cols = starts_with("mean_"), names_to = "subject", values_to = "score") %>% | |||
mutate(subject = recode(subject, "mean_math" = "Математика", "mean_science" = "Наука")) | |||
ggplot(data_long, aes(x = country, y = score, fill = gender)) + | |||
geom_col(position = "dodge") + | |||
facet_wrap(~ subject) + | |||
labs(title = "Средние взвешенные баллы PISA 2015", | |||
x = "Страна", y = "Средний балл", fill = "Пол") + | |||
theme_minimal() + | |||
theme(legend.position = "bottom") | |||
</syntaxhighlight> | |||
Версия от 17:46, 9 октября 2026
Здесь будет пример кода на языке R
getwd()
#------------
library(learningtower);
library(dplyr)
library(ggplot2)
#----
student_data_2015 <- load_student(2015)
student_data_2012 <- load_student(2012)
str(student_data_2015)
glimpse(student_data_2015)
summary(student_data_2015)
glimpse(student_data_2015)
data_proc <- student_data_2015 %>%
filter(country %in% c("RUS", "USA", "SGP")) %>%
group_by(country, gender) %>%
summarise(
mean_math = weighted.mean(math, stu_wgt, na.rm = TRUE),
mean_science = weighted.mean(science, stu_wgt, na.rm = TRUE),
n = n(),
.groups = "drop"
) %>%
na.omit()
print(data_proc)
ggplot(student_data_2015 %>% filter(country %in% c("RUS", "USA", "SGP")),
aes(x = country, y = math, fill = gender)) +
geom_boxplot(alpha = 0.7) +
labs(title = "Распределение баллов по математике (PISA 2015)",
x = "Страна", y = "Баллы по математике", fill = "Пол") +
theme_minimal() +
theme(legend.position = "bottom")
#----
ggplot(student_data_2012 %>% filter(country %in% c("RUS", "USA", "SGP")),
aes(x = country, y = math, fill = gender)) +
geom_boxplot(alpha = 0.7) +
labs(title = "Распределение баллов по математике (PISA 2012)",
x = "Страна", y = "Баллы по математике", fill = "Пол") +
theme_minimal() +
theme(legend.position = "bottom")
##---
data_long <- data_proc %>%
tidyr::pivot_longer(cols = starts_with("mean_"), names_to = "subject", values_to = "score") %>%
mutate(subject = recode(subject, "mean_math" = "Математика", "mean_science" = "Наука"))
ggplot(data_long, aes(x = country, y = score, fill = gender)) +
geom_col(position = "dodge") +
facet_wrap(~ subject) +
labs(title = "Средние взвешенные баллы PISA 2015",
x = "Страна", y = "Средний балл", fill = "Пол") +
theme_minimal() +
theme(legend.position = "bottom")
