Split a dataframe by data collection cycles
Source:R/split_data_by_cycles.R
split_data_by_cycles.RdDivides a survey dataframe that contains data from multiple data collection cycles (such as different years, rounds, or waves) into a list of dataframes, one for each cycle.
Usage
split_data_by_cycles(
data,
cycle_col,
name_prefix = "",
drop_cycle_col = TRUE,
drop_all_na_cols = FALSE
)Arguments
- data
A survey dataframe.
- cycle_col
Quoted or unquoted name of the column containing cycle information.
- name_prefix
A string prefix to add before each cycle name to form the names of the resulting list elements.
- drop_cycle_col
Logical, whether to drop the cycle column from each resulting dataframe.
- drop_all_na_cols
Logical, determines whether to remove columns that contain only NA values for a given cycle. This is useful when there are columns that represent questions asked only in certain cycles, thereby avoiding columns populated entirely with NA values in cycles where the question was not asked.
Value
A list of dataframes, each corresponding to a different data cycle.
The names of the list elements are created by combining name_prefix with
the values from the cycle column, cleaned using
janitor::make_clean_names(). For example, if name_prefix is "housing_"
and the cycle column values range from "Wave 1" to "Wave 7", the resulting
list names will be "housing_wave_1", "housing_wave_2", ...,
"housing_wave_7".
Examples
data_spl <-
split_data_by_cycles(
example_survey_data_housing,
cycle_col = wave,
name_prefix = "housing_",
drop_all_na_cols = TRUE
)
#>
#>
#> Building packages ■■■■■ 12% | ETA: 2m
#>
#> ── housing_wave_1 ──
#> Building packages ■■■■■ 12% | ETA: 2m
#>
#>
#> Building packages ■■■■■ 12% | ETA: 2m
#>
#> ℹ Dropping columns with all NA values: "q18" and "q13".
#> Building packages ■■■■■ 12% | ETA: 2m
#>
#>
#> Building packages ■■■■■ 12% | ETA: 2m
#>
#> ── housing_wave_2 ──
#> Building packages ■■■■■ 12% | ETA: 2m
#>
#>
#> Building packages ■■■■■ 12% | ETA: 2m
#>
#> ℹ Dropping columns with all NA values: "q18".
#> Building packages ■■■■■ 12% | ETA: 2m