Description Usage Arguments Details Value Examples
cases
allows to simultaneously several cases determined
by logical conditions. It can be used to code these case into
a factor or as a multi-condition generalization of ifelse
1 |
... |
A sequence of logical expressions or assignment expressions containing logical expressions as "right hand side". |
check.xor |
character (either |
There are two distinct ways to use this function. Either the
function can be used to construct a factor that represents
several logical cases or it can be used to conditionally
evaluate an expression in a manner similar to ifelse
.
For the first use, the ...
arguments have to be a series of
logical expressions. cases
then returns a factor
with as many levels as logical expressions given as
...
arguments. The resulting factor will attain its
first level if the first condition is TRUE, otherwise it will attain its
second level if the second condition is TRUE, etc.
The levels will be named after the conditions or, if name tags are
attached to the logical expressions, after the tags of the expressions.
Not that the logical expressions all need to evaluate to logical vectors
of the same length, otherwise an error condition is raised.
For the second use, the ...
arguments have to be a series
of assignment expression of the type <expression> <- <logical expression>
or <logical expression> -> <expression>
. For cases
in which the first logical expression is TRUE, the result of first expression that
appears on the other side of the assignment operator become elements of the
vector returned by cases
, for cases in which the second logical expression is TRUE,
the result of the second expression that appears on the other side
of the assignment operator become elements of the
vector returned by cases
, etc.
Note that the logical expressions also here all need to evaluate to logical
vectors of the same length. The expressions on the other side of the
assignment operator should also be either vectors of the same length
and mode or should scalars of the same mode, otherwise unpredictable
results may occur.
If it is called with logical expressions as ... arguments,
cases
returns a factor, if it is called with
assignment expressions the function returns a vector with the
same mode as the results of the "assigned" expressions
and with the same length as the logical conditions.
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 | # Examples of the first kind of usage of the function
#
df <- data.frame(x = rnorm(n=20), y = rnorm(n=20))
df <- df[do.call(order,df),]
(df <- within(df,{
x1=cases(x>0,x<=0)
y1=cases(y>0,y<=0)
z1=cases(
"Condition 1"=x<0,
"Condition 2"=y<0,# only applies if x >= 0
"Condition 3"=TRUE
)
z2=cases(x<0,(x>=0 & y <0), (x>=0 & y >=0))
}))
xtabs(~x1+y1,data=df)
dd <- with(df,
try(cases(x<0,
x>=0,
x>1,
check.xor=TRUE)# let's be fussy
)
)
dd <- with(df,
try(cases(x<0,x>=0,x>1))
)
genTable(range(x)~dd,data=df)
# An example of the second kind of usage of the function:
# A construction of a non-smooth function
#
fun <- function(x)
cases(
x==0 -> 1,
abs(x)> 1 -> abs(x),
abs(x)<=1 -> x^2
)
x <- seq(from=-2,to=2,length=101)
plot(fun(x)~x)
|
Loading required package: lattice
Loading required package: MASS
Attaching package: 'memisc'
The following objects are masked from 'package:stats':
contr.sum, contr.treatment, contrasts
The following object is masked from 'package:base':
as.array
x y z2 z1 y1 x1
17 -1.57599753 1.80975804 x < 0 Condition 1 y > 0 x <= 0
13 -1.45499235 0.54133899 x < 0 Condition 1 y > 0 x <= 0
16 -1.43211227 -0.38359885 x < 0 Condition 1 y <= 0 x <= 0
6 -1.37968962 0.40993368 x < 0 Condition 1 y > 0 x <= 0
15 -0.38322323 0.86007727 x < 0 Condition 1 y > 0 x <= 0
4 -0.32717427 1.63877840 x < 0 Condition 1 y > 0 x <= 0
3 0.07216042 -1.61122633 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
1 0.21895817 -1.55460474 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
20 0.42620086 -0.37446288 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
8 0.67835513 -1.80845084 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
2 0.90770429 -0.81119189 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
12 1.08985221 -1.04662462 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
10 1.12572890 -0.68763065 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
14 1.30232429 0.18399673 (x >= 0 & y >= 0) Condition 3 y > 0 x > 0
18 1.30938755 0.37947238 (x >= 0 & y >= 0) Condition 3 y > 0 x > 0
5 1.32076208 -1.49789719 (x >= 0 & y < 0) Condition 2 y <= 0 x > 0
11 1.68046984 0.08053804 (x >= 0 & y >= 0) Condition 3 y > 0 x > 0
7 1.74566269 0.26617001 (x >= 0 & y >= 0) Condition 3 y > 0 x > 0
9 1.77339495 1.67372677 (x >= 0 & y >= 0) Condition 3 y > 0 x > 0
19 1.84781650 1.85845149 (x >= 0 & y >= 0) Condition 3 y > 0 x > 0
Warning message:
In cases(`Condition 1` = x < 0, `Condition 2` = y < 0, `Condition 3` = TRUE) :
conditions are not mutually exclusive
y1
x1 y > 0 y <= 0
x > 0 6 8
x <= 0 5 1
Error in cases(x < 0, x >= 0, x > 1, check.xor = TRUE) :
conditions are not mutually exclusive
Warning message:
In cases(x < 0, x >= 0, x > 1) : conditions are not mutually exclusive
dd
x < 0 x >= 0
Min -1.57599753 0.07216042
Max -0.32717427 1.84781650
Warning message:
In cases(1 <- x == 0, abs(x) <- abs(x) > 1, x^2 <- abs(x) <= 1) :
conditions are not mutually exclusive
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.