memisc (version 0.11-9)

cases: Code several logical conditions into a factor

Description

cases takes a sequence of logical expressions and codes them into levels of a factor. It may be seen as a generalisatin of ifelse()

Usage

cases(...)

Arguments

...
A sequence of logical expressions

Value

  • A factor with a number of levels equal to the number of logical expressions given as arguments and labels equal to the (deparsed) logical expressions or names given to the arguments.

Details

The logical expressions are tested sequentially. If the first expression is true then the resulting factor attains its first level, otherwise if the second expression is true then teh resulting factor attains its second level, etc. Note that if none of the expressions is true, the resulting factor gets NA. One can, however specify a default condition by TRUE as the last argument to this function.

This should be useful for some more complex recodings, for example for index construction.

Examples

Run this code
df <- data.frame(x = rnorm(n=40), y = rnorm(n=40))
df <- df[do.call(order,df),]
(df <- transform(df,
  x.2=cases(x>0,x<=0),
  y.2=cases(y>0,y<=0),
  z1=cases(
    "Condition 1"=x<0,
    "Condition 2"=(x>=0 & y <0),
    "Condition 3"=TRUE
    ),
  z2=cases(x<0,(x>=0 & y <0), (x>=0 & y >=0))
  ))
xtabs(~x.2+y.2,data=df)

Run the code above in your browser using DataLab