Recently Published
PSYCH 251: Final Project
Replication of Muenks et al's paper
Gasoline Prices in New York State – Weekly and Quarterly Trends
This code sets up a two-row plotting layout with par(mfrow = c(2,1)) and increases the bottom margin using mar = c(6,4,4,2) to make space for text. The first plot displays weekly gasoline prices in New York State as a red line, with a descriptive paragraph added below using mtext() to explain the dataset, the observed trends in 2025, the units in dollars per gallon, and the weekly variations across the state. The second plot shows quarterly average prices in green, again with a paragraph added below describing that the data is aggregated from weekly values, highlighting smoothed trends, units, and long-term price movements in New York State. The cex = 0.8 parameter reduces the text size so the paragraphs fit neatly under the plots.
таблиця Розподіл респондентів за групами посад (кількість осіб, %
library(dplyr)
library(flextable)
library(officer)
N <- 30 # загальна кількість респондентів
tbl_roles <- tibble::tibble(
`Група посад` = c(
"Адміністративні та допоміжні ролі",
"Медичні фахівці (невролог, психіатр)",
"Педагогічні працівники / освітні фахівці",
"Працівники органів соціального захисту / державних служб",
"Юристи / правові фахівці",
"Менеджери програм, координатори, керівники підрозділів",
"Психологи",
"Фахівці із соціальної роботи / соціальні працівники"
),
n = c(6, 2, 2, 5, 3, 10, 11, 21)
) %>%
mutate(`%` = round(n / N * 100, 1))
ft <- flextable(tbl_roles) %>%
set_caption("Таблиця 2.1. Розподіл респондентів за групами посад (кількість осіб, %)") %>%
align(j = 1, align = "left") %>%
align(j = c("n", "%"), align = "center") %>%
autofit()
ft
# Експорт у Word
doc <- read_docx() %>%
body_add_flextable(ft)
print(doc, target = "roles_table_with_percent.docx")
Document
DAÑOS DE LA NATURALEZA
table questionnaire 2.1
# --- 0) Packages ---
packs <- c("readr", "dplyr", "flextable", "officer", "rlang")
new_packs <- packs[!(packs %in% installed.packages()[,"Package"])]
if(length(new_packs)) install.packages(new_packs)
lapply(packs, library, character.only = TRUE)
# --- 1) Read data ---
df <- readr::read_csv(
"C:/Users/selfi/OneDrive/Desktop/analysis_data_masters.csv",
show_col_types = FALSE
)
# --- 2) Rename columns with spaces (IMPORTANT) ---
df <- df %>%
dplyr::rename(
sex = sex,
type_org = `type of org`,
general_exp = `general exp`,
current_exp = `current exp`,
supervision = supervision,
IDP_status = `IDP status`,
disabilitiy = disabilitiy,
care = care,
household = household
)
# --- 3) Recode: 1 = Так, 2 = Ні (IDP, disability, care, household) ---
df <- df %>%
dplyr::mutate(
IDP_status = factor(IDP_status, levels = c(1, 2), labels = c("Так", "Ні")),
disabilitiy = factor(disabilitiy, levels = c(1, 2), labels = c("Так", "Ні")),
care = factor(care, levels = c(1, 2), labels = c("Так", "Ні")),
household = factor(household, levels = c(1, 2), labels = c("Так", "Ні"))
)
# --- 4) Helper: frequency block for one variable (keeps factor order) ---
freq_block <- function(data, var, indicator_label) {
v <- rlang::sym(var)
data %>%
dplyr::filter(!is.na(!!v), as.character(!!v) != "") %>%
dplyr::count(!!v, name = "n", .drop = FALSE) %>% # keep factor order
dplyr::mutate(
`%` = round(100 * n / sum(n), 1),
Показник = indicator_label,
Категорія = as.character(!!v)
) %>%
dplyr::select(Показник, Категорія, n, `%`)
}
# --- 5) Build full table ---
table_data <- dplyr::bind_rows(
freq_block(df, "sex", "Стать"),
freq_block(df, "type_org", "Тип організації"),
freq_block(df, "general_exp", "Загальний стаж"),
freq_block(df, "current_exp", "Поточний стаж"),
freq_block(df, "supervision", "Частота супервізії"),
freq_block(df, "IDP_status", "Статус ВПО"),
freq_block(df, "disabilitiy", "Інвалідність"),
freq_block(df, "care", "Потреба в догляді"),
freq_block(df, "household", "Домогосподарство")
)
# --- 6) Flextable formatting (like screenshot) ---
ft <- flextable::flextable(table_data) %>%
flextable::set_caption("Анкетні дані") %>%
flextable::merge_v(j = "Показник") %>%
flextable::bold(j = "Показник", bold = TRUE) %>%
flextable::align(j = c("Показник","Категорія"), align = "left", part = "all") %>%
flextable::align(j = c("n","%"), align = "center", part = "all") %>%
flextable::autofit() %>%
flextable::border_remove() %>%
flextable::border_outer(border = fp_border(width = 1)) %>%
flextable::border_inner_h(border = fp_border(width = 1)) %>%
flextable::border_inner_v(border = fp_border(width = 1))
# Preview in Viewer
ft
# --- 7) Export to Word ---
doc <- officer::read_docx() %>%
officer::body_add_flextable(ft)
print(doc, target = "anketni_dani.docx")