Decompose a survey dataframe for database upload
Source:R/srv_decompose_for_db.R
srv_decompose_for_db.RdDecomposes a survey dataframe into four tables ready for database insertion:
surveys: This component forms a dataframe representing the high-level metadata for the survey being decomposed. As each survey corresponds to a single entry in the database's
surveystable, this component will contain exactly one row.respondents: Each row represents one respondent, containing respondent IDs and their weights.
questions: This table is generated by applying
get_dict_for_db()to the input data frame, excluding ID and weight columns. Each remaining column results in an entry here. Useassign_col_*()functions to set question attributes.answers: A table where each row represents a respondent's answer to one question, storing responses both as text (
answer_chr) and as numeric values (answer_num).
Arguments
- data
A survey data frame.
- respondent_id_col
The column name, quoted or unquoted, for respondent IDs, typically "response_id" or "caseid".
- weight_col
The column name, quoted or unquoted, for respondent weights.
- survey_id
A short camel-case identifier for the survey, used in the database, such as "housing_wave_1".
- survey_name
A longer title-case string for the survey name used in the database, like "CLIENT NAME Housing Wave 1".
- date_start
Optional, the start date of the survey collection period.
- date_end
Optional, the end date of the survey collection period.
Examples
data_marked <-
example_survey_data_climate |>
assign_col_module("demographics", XIDEO, XPARTY7) |>
assign_col_module("questionnaire", starts_with("Q")) |>
assign_col_response_type("multiple_response", starts_with("Q17_Q16")) |>
assign_col_response_type("single_response", XIDEO, XPARTY7, Q11) |>
assign_col_mr_group("Q17_Q16", starts_with("Q17_Q16"))
decomposition <-
srv_decompose_for_db(
data_marked,
respondent_id_col = response_id,
weight_col = weight,
survey_id = "climate",
survey_name = "Opinions on Climate",
date_start = as.Date("2022-10-12"),
date_end = as.Date("2022-10-14")
)