library(haven)    # For reading .sav files
library(dplyr)    # For data manipulation
library(tidyr)    # For reshaping data
library(janitor)  # For cleaning data

1 Processing

Mendefinisikan lansia

data <- data %>%
  mutate(
    RT = ifelse(R407 >= 60, 1, 0)  # RT = 1 if R407 >= 60
  )

Agregate lansia per rumah tangga (membentuk RT_sum)

# Aggregate: sum of RT grouped by URUT
agg_data <- data %>%
  group_by(URUT) %>%
  summarize(
    RT_sum = sum(RT, na.rm = TRUE),
    .groups = "drop"
  )

Merge data agregate lansia dengan data awal

# Merge aggregated data back to the original data
data <- left_join(data, agg_data, by = c("URUT"))

Mendefinisikan jika dalam 1 ruta terdapat lebih dari 1 lansia, diberi nilai 100

data <- data %>%
  mutate(
    RT = ifelse(RT_sum > 0, 100, RT)
  )
# Add variable labels
# Labels are added manually
attr(data$R105, "label") <- "Tipe Daerah"
attr(data$R405, "label") <- "Jenis Kelamin"

Select unit analisis sebagai rumah tangga

# Filter the data where R403 = 1
filtered_data <- data %>%
  filter(R403 == 1)
library(survey)
survey_design <- svydesign(
  ids = ~1,                # No clustering (simple random sampling)
  weights = ~FWT,          
  data = filtered_data     
)

weighted_table <- svyby(
  ~RT,                     # Variable to analyze
  ~R101+R105,              # Grouping variables
  survey_design,           # Survey design object
  svymean,                 # Function for weighted mean
  na.rm = TRUE             # Remove missing values
)
# Generating tables (replace this with your desired table output method)
table <- weighted_table %>%
  group_by(R101, R105) %>%
  summarize(
    mean_RT = mean(RT, na.rm = TRUE),  # Calculating mean of RT
    .groups = "drop"                   # Remove grouping after summarization
  ) %>%
  arrange(R101, R105)

2 Output

library(knitr)
library(kableExtra)
# Mengatur nilai mean_RT ke 2 digit desimal
table <- table %>%
  mutate(mean_RT = round(mean_RT, 2))  # Membulatkan ke 2 desimal

# Creating a table with `R101` as rows and `mean_RT` as values
table %>%
  pivot_wider(names_from = R105, values_from = mean_RT, values_fill = list(mean_RT = NA)) %>%
  kable(caption = "Rata-Rata Ruta dengan Lansia menurut Provinsi dan Tipe Daerah, 2024") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                full_width = F, position = "center") %>%
  add_header_above(c("Provinsi" = 1, "Tipe Daerah" = ncol(table) - 1)) %>%
  footnote(general = "Sumber: Susenas KOR, 2024", 
           general_title = "Catatan:")
Rata-Rata Ruta dengan Lansia menurut Provinsi dan Tipe Daerah, 2024
Provinsi
Tipe Daerah
R101 1 2
11 27.23 30.32
12 30.90 31.65
13 31.56 34.53
14 23.61 24.24
15 31.31 30.96
16 32.06 29.74
17 27.67 32.60
18 31.66 33.34
19 28.63 29.27
21 17.56 34.69
31 29.35 NA
32 29.17 35.47
33 41.35 42.35
34 34.62 51.87
35 41.60 44.76
36 26.00 37.73
51 33.82 43.00
52 27.76 27.74
53 30.65 35.40
61 32.82 29.95
62 23.86 26.41
63 31.01 33.14
64 26.57 31.59
65 27.22 33.31
71 37.30 40.50
72 28.77 31.95
73 35.60 41.64
74 25.79 31.53
75 34.02 34.30
76 31.67 29.35
81 30.04 35.02
82 27.50 30.95
91 21.45 20.19
92 22.19 26.52
94 32.82 33.65
95 26.19 25.66
96 18.73 10.35
97 11.66 10.35
Catatan:
Sumber: Susenas KOR, 2024

Welfare Statistics Directorate, BPS,