Movatterモバイル変換


[0]ホーム

URL:


levels {base}R Documentation

Levels Attributes

Description

levels provides access to the levels attribute of a variable.The first form returns the value of the levels of its argumentand the second sets the attribute.

Usage

levels(x)levels(x) <- value

Arguments

x

an object, for example a factor.

value

a valid value forlevels(x).For the default method,NULL or a character vector. For thefactor method, a vector of character strings with length atleast the number of levels ofx, or a named list specifying how torename the levels.

Details

Both the extractor and replacement forms are generic and new methodscan be written for them. The most important method for the replacementfunction is that forfactors.

For the factor replacement method, aNA invaluecauses that level to be removed from the levels and the elementsformerly with that level to be replaced byNA.

Note that for a factor, replacing the levels vialevels(x) <- value is not the same as (and is preferred to)attr(x, "levels") <- value.

The replacement function isprimitive.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

nlevels,relevel,reorder.

Examples

## assign individual levelsx <- gl(2, 4, 8)levels(x)[1] <- "low"levels(x)[2] <- "high"x## or as a groupy <- gl(2, 4, 8)levels(y) <- c("low", "high")y## combine some levelsz <- gl(3, 2, 12, labels = c("apple", "salad", "orange"))zlevels(z) <- c("fruit", "veg", "fruit")z## same, using a named listz <- gl(3, 2, 12, labels = c("apple", "salad", "orange"))zlevels(z) <- list("fruit" = c("apple","orange"),                  "veg"   = "salad")z## we can add levels this way:f <- factor(c("a","b"))levels(f) <- c("c", "a", "b")ff <- factor(c("a","b"))levels(f) <- list(C = "C", A = "a", B = "b")f

[Packagebase version 4.6.0Index]

[8]ページ先頭

©2009-2025 Movatter.jp