File: assign-if.Rd

package info (click to toggle)
r-cran-memisc 0.99.31.8.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,136 kB
  • sloc: ansic: 5,117; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
\name{assign_if}
\alias{assign_if}
\alias{\%if\%}
\title{Assign a values to a variable for instances where a condition is
  met}
\description{
  The \code{\%if\%} operator allows to assign values to a variable only if
    a condition is met i.e. results in \code{TRUE}. It is supposed to
    be used similar to the \code{replace ... if} construct in Stata.
}
\usage{
expr \%if\% condition
# For example
# (variable <- value) \%if\% (other_variable == 0)
}
\arguments{
  \item{expr}{An expression that assigns a value to variable}
  \item{condition}{A logical vector or a an expression that evaluates
  to a logical vector}
}
\details{
  The 'value' that is assigned to the variable in \code{expr}
  should either be a scalar, a vector with as many elements as the
  condition vector has, or as many elements as the number of elements
  in the condition vector that are equal (or evaluate to) \code{TRUE}. 
}
\examples{
(test_var <- 1) \%if\% (1:7 > 3)

test_var

(test_var <- 2) \%if\% (1:7 <= 3)
test_var

(test_var <- 100*test_var) \%if\% (1:7\%\%2==0)
test_var

# This creates a warning about non-matching lengths.
(test_var <- 500:501) \%if\% (1:7 <= 3)
test_var

(test_var <- 501:503) \%if\% (1:7 <= 3)
test_var

(test_var <- 401:407) \%if\% (1:7 <= 3)
test_var
}