Split up a data frame based on variable expressions or on conditions to receive multiple smaller data frames. Both possibilities can be used at the same time.
Usage
split_by(
data_frame,
...,
formats = list(),
inverse = FALSE,
monitor = .qol_options[["monitor"]]
)Arguments
- data_frame
A data frame which should be split up into multiple data frames.
- ...
Pass in one or multiple variables and/or conditions on which the provided data frame should be splitted.
- formats
A list in which is specified which formats should be applied to which variables. Macro variables can be used in the variable names and format names.
- inverse
Uses the inverse conditions to split up the data frame.
- monitor
FALSE by default. If TRUE, outputs two charts to visualize the functions time consumption.
Value
Returns a list of data frames split by variable expressions and/or conditions. The lists names are the variable expressions or conditions.
Details
split_by() is based on the explicit Output from 'SAS'. With the Output function
one can - among other things - explicitly tell 'SAS' which observation to output into
which data set. Which enables the user to output one observation into one or multiple
data sets.
Instead of subsetting the same data frame multiple times manually, you can subset it multiple times at once with this function.
The function can take in conditions as characters to be able to parse the conditions. The parsing allows to write conditions in a SAS like way. For example the condition
"age >= 15 & age < 65" can be written as "15 <= age < 65"
Additionally and/or (case insensitive) are recognized keywords which will be translated into &/|.
"age >= 15 and age < 65 or sex == 1" becomes "age >= 15 & age < 65 | sex == 1"
And macro() variables are allowed in any spot, e.g.:
YEAR <- 2025\ "year == &YEAR" translates to "year == 2025".
Single = get translated to ==, the SAS not ^= is translated to !=.
In SAS you can write "age in (1 4 7 15 21)" or "age not in (2, 4, 37, 82)" which gets translated into "age %in% (1, 4, 7, 15, 21)" and "!(age %in% (2, 4, 37, 82))".
Conditions can also be negated with "not (condition)", which gets translated into "!(condition)", e.g. "not (age >= 15 and age < 65)".
When using == or != operators you can check if character expressions start with (text:), end with (:text) or contain a certain text (:text:). These also get translated.
Examples
# Example data frame
my_data <- dummy_data(1000)
# Split by variable expressions
split_var_df <- my_data |> split_by(sex)
# Split by conditions
split_cond_df <- my_data |> split_by(sex == 1 & age < 18,
sex == 2 & age >= 18)
# Split by character conditions with 'SAS' like syntax
split_cond_df <- my_data |> split_by(" 15 <= age < 65 ",
" education in ('low' 'middle') ")
# Split by condition with inverse group
split_inv_df <- my_data |> split_by(sex == 1, inverse = TRUE)
# Split by variables and conditions
split_combi_df <- my_data |> split_by(state, education,
sex == 1, age < 18)
# Split by variable expressions using formats
state. <- discrete_format(
"Germany" = 1:16,
"Schleswig-Holstein" = 1,
"Hamburg" = 2,
"Lower Saxony" = 3,
"Bremen" = 4,
"North Rhine-Westphalia" = 5,
"Hesse" = 6,
"Rhineland-Palatinate" = 7,
"Baden-Württemberg" = 8,
"Bavaria" = 9,
"Saarland" = 10,
"West" = 1:10,
"Berlin" = 11,
"Brandenburg" = 12,
"Mecklenburg-Western Pomerania" = 13,
"Saxony" = 14,
"Saxony-Anhalt" = 15,
"Thuringia" = 16,
"East" = 11:16)
split_format_df <- my_data |> split_by(state,
formats = list(state = state.))
