Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

case

Map elements of a vector according to the provided 'cases'


Description

Map elements of a vector according to the provided 'cases'. This function is useful for mapping discrete values to factor labels and is the vector equivalent to the switch function.

Usage

case(x, ..., default = NA)

Arguments

x

Vector to be converted

...

Map of alternatives, specified as "name"=value

default

Value to be assigned to elements of x not matching any of the alternatives. Defaults to NA.

Details

This function is to switch what ifelse is to if, and is a convenience wrapper for factor.

Value

A factor variables with each element of x mapped into the corresponding level of specified in the mapping.

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

factor, switch, ifelse

Examples

## default = NA
case( c(1,1,4,3), "a"=1, "b"=2, "c"=3)

## default = "foo"
case( c(1,1,4,3), "a"=1, "b"=2, "c"=3, default="foo" )

gdata

Various R Programming Tools for Data Manipulation

v2.18.0
GPL-2
Authors
Gregory R. Warnes, Ben Bolker, Gregor Gorjanc, Gabor Grothendieck, Ales Korosec, Thomas Lumley, Don MacQueen, Arni Magnusson, Jim Rogers, and others
Initial release
2017-06-05

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.