Creates a filter variable based on the given condition. This variable can be
accessed by if.(), else_if.(), else.(), where.() and compute(), enabling these
functions to work with an overarching condition. This function can also be
used to nest multiple overarching conditions.
else_do(): Checks for existing filter variables and reverses the condition
of the last filter variable.
end_do(): Drops the last filter variable.
end_all_do(): Drops all filter variables.
See also
The following functions can make use of the do_if() filter variables:
Conditions: if.(), else_if.(), else.()
Filter Data Frame: where.()
Create new Variables: compute()
Examples
# Example data frame
my_data <- dummy_data(1000)
# Create a simple do-if-block
do_if_df <- my_data |>
do_if(state < 11) |>
if.(age < 18, new_var = 1) |>
else.( new_var = 2) |>
else_do() |>
if.(age < 18, new_var = 3) |>
else.( new_var = 4) |>
end_do()
# do_if() can also be nested
do_if_df <- my_data |>
do_if(state < 11) |>
do_if(sex == 1) |>
if.(age < 18, new_var = 1) |>
else.( new_var = 2) |>
else_do() |>
if.(age < 18, new_var = 3) |>
else.( new_var = 4) |>
end_do() |>
else_do() |>
do_if(sex == 1) |>
if.(age < 18, new_var = 5) |>
else.( new_var = 6) |>
else_do() |>
if.(age < 18, new_var = 7) |>
else.( new_var = 8) |>
end_do() |>
end_do()
# NOTE: Close the do-if-blocks with end_do() to remove the temporary logical
# filter variables.
# Probably a logical filter variable is exactly what you want. In this case
# just run do_if() without closing the block.
logic_filter_df <- my_data |> do_if(state < 11)
