The R Project SVN R

Rev

Rev 24300 | Rev 36992 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 24300 Rev 25364
Line 1... Line 1...
1
\name{levels}
1
\name{levels}
-
 
2
\alias{levels}
-
 
3
\alias{levels<-}
-
 
4
\alias{levels<-.default}
-
 
5
\alias{levels<-.factor}
2
\title{Levels Attributes}
6
\title{Levels Attributes}
3
\usage{
7
\usage{
4
levels(x)
8
levels(x)
5
levels(x) <- value
9
levels(x) <- value
6
}
10
}
7
\alias{levels}
-
 
8
\alias{levels<-}
-
 
9
\alias{levels<-.default}
-
 
10
\description{
11
\description{
11
  \code{levels} provides access to the levels attribute of a variable.
12
  \code{levels} provides access to the levels attribute of a variable.
12
  The first form returns the value of the levels of its argument
13
  The first form returns the value of the levels of its argument
13
  and the second sets the attribute.
14
  and the second sets the attribute.
14
 
15
 
Line 17... Line 18...
17
  The most important method is that for \code{\link{factor}}s:
18
  The most important method is that for \code{\link{factor}}s:
18
}
19
}
19
\arguments{
20
\arguments{
20
  \item{x}{an object, for example a factor.}
21
  \item{x}{an object, for example a factor.}
21
  \item{value}{A valid value for \code{levels(x)}.
22
  \item{value}{A valid value for \code{levels(x)}.
22
    For the default method, \code{NULL} or a character vector.}
23
    For the default method, \code{NULL} or a character vector.  For the
-
 
24
    \code{factor} method, a vector of character strings with length at
-
 
25
    least the number of levels of \code{x}, or a named list specifying how to
-
 
26
    rename the levels.}
23
}
27
}
24
\references{
28
\references{
25
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
29
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
26
  \emph{The New S Language}.
30
  \emph{The New S Language}.
27
  Wadsworth \& Brooks/Cole.
31
  Wadsworth \& Brooks/Cole.
28
}
32
}
-
 
33
\seealso{
29
\seealso{\code{\link{levels<-.factor}}, \code{\link{nlevels}}.
34
  \code{\link{nlevels}}.
-
 
35
}
-
 
36
\examples{
-
 
37
## assign individual levels
-
 
38
x <- gl(2, 4, 8)
-
 
39
levels(x)[1] <- "low"
-
 
40
levels(x)[2] <- "high"
-
 
41
x
-
 
42
 
-
 
43
## or as a group
-
 
44
y <- gl(2, 4, 8)
-
 
45
levels(y) <- c("low", "high")
-
 
46
y
-
 
47
 
-
 
48
## combine some levels
-
 
49
z <- gl(3, 2, 12)
-
 
50
levels(z) <- c("A", "B", "A")
-
 
51
z
-
 
52
 
-
 
53
## same, using a named list
-
 
54
z <- gl(3, 2, 12)
-
 
55
levels(z) <- list(A=c(1,3), B=2)
-
 
56
z
-
 
57
 
-
 
58
## we can add levels this way:
-
 
59
f <- factor(c("a","b"))
-
 
60
levels(f) <- c("c", "a", "b")
-
 
61
f
-
 
62
 
-
 
63
f <- factor(c("a","b"))
-
 
64
levels(f) <- list(C="C", A="a", B="b")
-
 
65
f
30
}
66
}
31
\keyword{category}
67
\keyword{category}