% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/if_else.R
\name{do_if}
\alias{do_if}
\alias{else_do}
\alias{end_do}
\alias{end_all_do}
\title{Lock In A Condition}
\usage{
do_if(data_frame, condition)

else_do(data_frame)

end_do(data_frame)

end_all_do(data_frame)
}
\arguments{
\item{data_frame}{A data frame on which to apply filters.}

\item{condition}{The condition lock in.}
}
\value{
Returns a data frame with a new filter variable.
}
\description{
Creates a filter variable based on the given condition. This variable can be
accessed by \code{\link[=if.]{if.()}}, \code{\link[=else_if.]{else_if.()}}, \code{\link[=else.]{else.()}}, \code{\link[=where.]{where.()}} and \code{\link[=compute.]{compute.()}}, enabling these
functions to work with an overarching condition. This function can also be
used to nest multiple overarching conditions.

\code{\link[=else_do]{else_do()}}: Checks for existing filter variables and reverses the condition
of the last filter variable.

\code{\link[=end_do]{end_do()}}: Drops the last filter variable.

\code{\link[=end_all_do]{end_all_do()}}: Drops all filter variables.
}
\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)

}
\seealso{
The following functions can make use of the \code{\link[=do_if]{do_if()}} filter variables:

Conditions: \code{\link[=if.]{if.()}}, \code{\link[=else_if.]{else_if.()}}, \code{\link[=else.]{else.()}}

Filter Data Frame: \code{\link[=where.]{where.()}}

Create new Variables: \code{\link[=compute.]{compute.()}}
}
