# List of packages
packages <- c("tidyverse", "gapminder", "fst", "viridis", "ggridges", "modelsummary","packrat","rsconnect")
# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 载入需要的程序包:viridisLite
##
## `modelsummary` 2.0.0 now uses `tinytable` as its default table-drawing
## backend. Learn more at: https://vincentarelbundock.github.io/tinytable/
##
## Revert to `kableExtra` for one session:
##
## options(modelsummary_factory_default = 'kableExtra')
## options(modelsummary_factory_latex = 'kableExtra')
## options(modelsummary_factory_html = 'kableExtra')
##
## Silence this message forever:
##
## config_modelsummary(startup_message = FALSE)
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "gapminder" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "fst" "gapminder" "lubridate" "forcats" "stringr" "dplyr"
## [7] "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
##
## [[4]]
## [1] "viridis" "viridisLite" "fst" "gapminder" "lubridate"
## [6] "forcats" "stringr" "dplyr" "purrr" "readr"
## [11] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [16] "graphics" "grDevices" "utils" "datasets" "methods"
## [21] "base"
##
## [[5]]
## [1] "ggridges" "viridis" "viridisLite" "fst" "gapminder"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[6]]
## [1] "modelsummary" "ggridges" "viridis" "viridisLite" "fst"
## [6] "gapminder" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[7]]
## [1] "packrat" "modelsummary" "ggridges" "viridis" "viridisLite"
## [6] "fst" "gapminder" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[8]]
## [1] "rsconnect" "packrat" "modelsummary" "ggridges" "viridis"
## [6] "viridisLite" "fst" "gapminder" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
library(fst)
ess <- read_fst("All-ESS-Data.fst")
table(ess$happy)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 3933 3540 7343 13759 17016 52117 43707 87869 130802 76229 51006
## 77 88 99
## 330 2163 741
germany_data <- ess %>%
filter(cntry == "DE") %>%
mutate(
happy = ifelse(happy == 2, 0, ifelse(happy %in% c(77, 88, 99), NA, happy)),
stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem)
)
table(germany_data$happy)
##
## 0 1 3 4 5 6 7 8 9 10
## 723 196 925 1072 3227 2725 6281 10210 5527 3345
table(germany_data$stfdem)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1475 812 1943 3170 3333 5062 3996 5545 5400 2105 1081
datasummary_skim(germany_data %>% select(happy, stfdem))
| Unique | Missing Pct. | Mean | SD | Min | Median | Max | Histogram | |
|---|---|---|---|---|---|---|---|---|
| happy | 11 | 1 | 7.3 | 2.0 | 0.0 | 8.0 | 10.0 | |
| stfdem | 12 | 1 | 5.6 | 2.5 | 0.0 | 6.0 | 10.0 |
library(tidyverse)
germany_happy <- ess %>%
filter(cntry == "DE") %>%
select(happy)
germany_happy$y <- germany_happy$happy
table(germany_happy$y)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 198 196 525 925 1072 3227 2725 6281 10210 5527 3345 25 47
## 99
## 122
germany_happy$y[germany_happy$y %in% 77:99] <- NA
table(germany_happy$y)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 198 196 525 925 1072 3227 2725 6281 10210 5527 3345
mean_y <- mean(germany_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.292629
# Now, let's determine the median of 'y'.
median_y <- median(germany_happy$y, na.rm = TRUE)
cat("Median of 'y' is:", median_y, "\n")
## Median of 'y' is: 8
# Using tidyverse syntax principles, we can get both the mean and median at once.
germany_happy %>%
summarize(
mean_y = mean(y, na.rm = TRUE),
median_y = median(y, na.rm = TRUE)
) %>%
print()
## mean_y median_y
## 1 7.292629 8
ggplot(germany_data %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy)),
aes(x = happy)) +
geom_histogram(breaks = seq(0, 10, by = 1)) +
scale_x_continuous(breaks = 0:10, limits = c(0,10)) +
labs(title = "Histogram of Happiness for Germany citizens",
x = "happy",
y = "Count")
## Warning: Removed 194 rows containing non-finite outside the scale range
## (`stat_bin()`).
The data visualization indicates that a significant majority of individuals in Germany report feeling extremely happy, as evidenced by approximately 10,000 respondents selecting a score of 8 for this survey question.Based on the above statistics, it is evident that the average level of happiness among people in Germany is approximately 7.29, with a median score of 8. This indicates a relatively high level of satisfaction, especially considering that the highest possible score is 10.
print('I did it!')
## [1] "I did it!"