| 1 |
\name{assign_if} |
| 2 |
\alias{assign_if} |
| 3 |
\alias{\%if\%} |
| 4 |
\title{Assign a values to a variable for instances where a condition is |
| 5 |
met} |
| 6 |
\description{ |
| 7 |
The \code{\%if\%} operator allows to assign values to a variable only if |
| 8 |
a condition is met i.e. results in \code{TRUE}. It is supposed to |
| 9 |
be used similar to the \code{replace ... if} construct in Stata. |
| 10 |
} |
| 11 |
\usage{ |
| 12 |
expr \%if\% condition |
| 13 |
# For example |
| 14 |
# (variable <- value) \%if\% (other_variable == 0) |
| 15 |
} |
| 16 |
\arguments{ |
| 17 |
\item{expr}{An expression that assigns a value to variable} |
| 18 |
\item{condition}{A logical vector or a an expression that evaluates |
| 19 |
to a logical vector} |
| 20 |
} |
| 21 |
\details{ |
| 22 |
The 'value' that is assigned to the variable in \code{expr} |
| 23 |
should either be a scalar, a vector with as many elements as the |
| 24 |
condition vector has, or as many elements as the number of elements |
| 25 |
in the condition vector that are equal (or evaluate to) \code{TRUE}. |
| 26 |
} |
| 27 |
\examples{ |
| 28 |
(test_var <- 1) \%if\% (1:7 > 3) |
| 29 |
|
| 30 |
test_var |
| 31 |
|
| 32 |
(test_var <- 2) \%if\% (1:7 <= 3) |
| 33 |
test_var |
| 34 |
|
| 35 |
(test_var <- 100*test_var) \%if\% (1:7\%\%2==0) |
| 36 |
test_var |
| 37 |
|
| 38 |
# This creates a warning about non-matching lengths. |
| 39 |
(test_var <- 500:501) \%if\% (1:7 <= 3) |
| 40 |
test_var |
| 41 |
|
| 42 |
(test_var <- 501:503) \%if\% (1:7 <= 3) |
| 43 |
test_var |
| 44 |
|
| 45 |
(test_var <- 401:407) \%if\% (1:7 <= 3) |
| 46 |
test_var |
| 47 |
} |