| 1 |
\name{cases} |
| 2 |
\alias{cases} |
| 3 |
\title{Distinguish between Cases Specified by Logical Conditions} |
| 4 |
\description{ |
| 5 |
\code{cases} allows to distinguish several cases defined logical |
| 6 |
conditions. It can be used to code these cases into a vector. The |
| 7 |
function can be considered as a multi-condition generalization of |
| 8 |
\code{\link{ifelse}}. |
| 9 |
} |
| 10 |
\usage{ |
| 11 |
cases(\dots,check.xor=c("warn","stop","ignore"), |
| 12 |
.default=NA,.complete=FALSE, |
| 13 |
check.na=c("warn","stop","ignore"), |
| 14 |
na.rm=TRUE) |
| 15 |
} |
| 16 |
\arguments{ |
| 17 |
\item{\dots}{A sequence of logical expressions or assignment expressions containing |
| 18 |
logical expressions as "right hand side".} |
| 19 |
\item{check.xor}{character (either \code{"warn"}, \code{"stop"}, or \code{"ignore"}) |
| 20 |
or logical; if \code{TRUE} or equal to \code{"stop"} or |
| 21 |
\code{"warn"}, |
| 22 |
\code{cases} checks whether the case |
| 23 |
conditions are mutually exclusive. If this is not |
| 24 |
satisfied and \code{check.xor} equals \code{"warn"} (the default), a warning is shown, |
| 25 |
otherwise an error exception is raised.} |
| 26 |
\item{.default}{a value to be used for unsatisfied conditions.} |
| 27 |
\item{.complete}{logical, if \code{TRUE} an additional factor level is |
| 28 |
created for the unsatisfied conditions.} |
| 29 |
\item{check.na}{character (either \code{"warn"}, \code{"stop"}, or \code{"ignore"}) |
| 30 |
or logical; if \code{TRUE} or equal to \code{"stop"} or |
| 31 |
\code{"warn"}, \code{cases} checks, whether any of the case |
| 32 |
conditions evaluates to \code{NA}. |
| 33 |
If that case, if \code{check.na} is \code{TRUE} or equals |
| 34 |
\code{"stop"} an error exception is raised, while if \code{check.na} |
| 35 |
equals \code{"warn"} (the default) a warning is shown.} |
| 36 |
\item{na.rm}{a logical value; how to handle \code{NA}s (if they do not |
| 37 |
already lead to an error exception). If \code{FALSE} if \emph{any} of the |
| 38 |
conditions evaluates to \code{NA}, the corresponding value of the |
| 39 |
result vector is \code{NA}. If \code{TRUE} (the default), the |
| 40 |
resulting vector or factor is \code{NA} only for instances where all |
| 41 |
conditions result in \code{NA}. |
| 42 |
} |
| 43 |
} |
| 44 |
\details{ |
| 45 |
There are two distinct ways to use this function. Either the |
| 46 |
function can be used to construct a factor that represents |
| 47 |
several logical cases or it can be used to conditionally |
| 48 |
evaluate an expression in a manner similar to \code{\link{ifelse}}. |
| 49 |
|
| 50 |
For the first use, the \code{\dots} arguments have to be a series of |
| 51 |
logical expressions. \code{cases} then returns a factor |
| 52 |
with as many levels as logical expressions given as |
| 53 |
\code{\dots} arguments. The resulting factor will attain its |
| 54 |
first level if the first condition is TRUE, otherwise it will attain its |
| 55 |
second level if the second condition is TRUE, etc. |
| 56 |
The levels will be named after the conditions or, if name tags are |
| 57 |
attached to the logical expressions, after the tags of the expressions. |
| 58 |
Not that the logical expressions all need to evaluate to logical vectors |
| 59 |
of the same length, otherwise an error condition is raised. |
| 60 |
If \code{.complete} is \code{TRUE} then an additional factor level is |
| 61 |
created for the conditions not satisfied for any of the cases. |
| 62 |
|
| 63 |
For the second use, the \code{\dots} arguments have to be a series |
| 64 |
of assignment expression of the type \code{<expression> <- <logical expression>} |
| 65 |
or \code{<logical expression> -> <expression>}. For cases |
| 66 |
in which the first logical expression is TRUE, the result of first expression that |
| 67 |
appears on the other side of the assignment operator become elements of the |
| 68 |
vector returned by \code{cases}, for cases in which the second logical expression is TRUE, |
| 69 |
the result of the second expression that appears on the other side |
| 70 |
of the assignment operator become elements of the |
| 71 |
vector returned by \code{cases}, etc. |
| 72 |
For cases that do not satisfy any of the given conditions the value of |
| 73 |
the \code{.default} argument is used. |
| 74 |
Note that the logical expressions also here all need to evaluate to logical |
| 75 |
vectors of the same length. The expressions on the other side of the |
| 76 |
assignment operator should also be either vectors of the same length |
| 77 |
and mode or should scalars of the same mode, otherwise unpredictable |
| 78 |
results may occur. |
| 79 |
|
| 80 |
} |
| 81 |
\value{ |
| 82 |
If it is called with logical expressions as \dots arguments, |
| 83 |
\code{cases} returns a factor, if it is called with |
| 84 |
assignment expressions the function returns a vector with the |
| 85 |
same mode as the results of the "assigned" expressions |
| 86 |
and with the same length as the logical conditions. |
| 87 |
} |
| 88 |
|
| 89 |
\examples{ |
| 90 |
# Examples of the first kind of usage of the function |
| 91 |
# |
| 92 |
df <- data.frame(x = rnorm(n=20), y = rnorm(n=20)) |
| 93 |
df <- df[do.call(order,df),] |
| 94 |
(df <- within(df,{ |
| 95 |
x1=cases(x>0,x<=0) |
| 96 |
y1=cases(y>0,y<=0) |
| 97 |
z1=cases( |
| 98 |
"Condition 1"=x<0, |
| 99 |
"Condition 2"=y<0,# only applies if x >= 0 |
| 100 |
"Condition 3"=TRUE |
| 101 |
) |
| 102 |
z2=cases(x<0,(x>=0 & y <0), (x>=0 & y >=0)) |
| 103 |
})) |
| 104 |
xtabs(~x1+y1,data=df) |
| 105 |
dd <- with(df, |
| 106 |
try(cases(x<0, |
| 107 |
x>=0, |
| 108 |
x>1, |
| 109 |
check.xor=TRUE)# let's be fussy |
| 110 |
) |
| 111 |
) |
| 112 |
dd <- with(df, |
| 113 |
try(cases(x<0,x>=0,x>1)) |
| 114 |
) |
| 115 |
genTable(range(x)~dd,data=df) |
| 116 |
|
| 117 |
# An example of the second kind of usage of the function: |
| 118 |
# A construction of a non-smooth function |
| 119 |
# |
| 120 |
fun <- function(x) |
| 121 |
cases( |
| 122 |
x==0 -> 1, |
| 123 |
abs(x)> 1 -> abs(x), |
| 124 |
abs(x)<=1 -> x^2 |
| 125 |
) |
| 126 |
x <- seq(from=-2,to=2,length=101) |
| 127 |
plot(fun(x)~x) |
| 128 |
|
| 129 |
# Demo of the new .default and .complete arguments |
| 130 |
x <- seq(from=-2,to=2) |
| 131 |
cases(a = x < -1, |
| 132 |
b = x > 1, |
| 133 |
.complete = TRUE) |
| 134 |
cases(x < -1, |
| 135 |
x > 1, |
| 136 |
.complete = TRUE) |
| 137 |
cases(1 <- x < -1, |
| 138 |
3 <- x > 1, |
| 139 |
.default = 2) |
| 140 |
|
| 141 |
threshhold <- 5 |
| 142 |
d <- c(1:10, NaN) |
| 143 |
|
| 144 |
d1 <- cases( |
| 145 |
d > threshhold -> 1, |
| 146 |
d <= threshhold -> 2 |
| 147 |
) |
| 148 |
|
| 149 |
d2 <- cases( |
| 150 |
is.na(d) -> 0, |
| 151 |
d > threshhold -> 1, |
| 152 |
d <= threshhold -> 2 |
| 153 |
) |
| 154 |
|
| 155 |
# Leads to missing values because some of the conditions result in missing |
| 156 |
# even though they could be 'captured' |
| 157 |
d3 <- cases( |
| 158 |
is.na(d) -> 0, |
| 159 |
d > threshhold -> 1, |
| 160 |
d <= threshhold -> 2, |
| 161 |
na.rm=FALSE |
| 162 |
) |
| 163 |
|
| 164 |
d4 <- cases( |
| 165 |
is.na(d) -> 0, |
| 166 |
d > threshhold +2 -> 1, |
| 167 |
d <= threshhold -> 2, |
| 168 |
na.rm=FALSE |
| 169 |
) |
| 170 |
|
| 171 |
cbind(d,d1,d2,d3,d4) |
| 172 |
|
| 173 |
cases( |
| 174 |
d > threshhold, |
| 175 |
d <= threshhold |
| 176 |
) |
| 177 |
|
| 178 |
cases( |
| 179 |
is.na(d), |
| 180 |
d > threshhold, |
| 181 |
d <= threshhold |
| 182 |
) |
| 183 |
|
| 184 |
cases( |
| 185 |
d > threshhold, |
| 186 |
d <= threshhold, |
| 187 |
.complete=TRUE |
| 188 |
) |
| 189 |
|
| 190 |
cases( |
| 191 |
d > threshhold + 2, |
| 192 |
d <= threshhold, |
| 193 |
.complete=TRUE |
| 194 |
) |
| 195 |
} |
| 196 |
|
| 197 |
\keyword{manip} |