class
attribute
factor
and a
levels
attribute
that determines the character strings to be included in the vector.
There is a big data analog; see
.
The function
factor
creates a factor object
out of data and allows one to set the
levels
attribute.
is.factor
determines whether a vector is a factor.
as.factor
coerces a vector into a factor.
factor(x, levels=<<see below>>, labels=<<see below>>, exclude=NA) is.factor(x) as.factor(x)
levels
).
Missing values (
NA
s) are allowed.
levels
will be
NA
in the factor.
The default value of
levels
is the sorted list
of distinct values of
x
.
(If
x
is numeric and contains NAs,
they will be placed at the end of the default value
for
levels
.)
If
x
is character data
or you want to exclude other values
from the
levels
you can use the
exclude
argument.
x
and
exclude
will be
NA
in the result
and it will not appear in the default
levels
attribute.
"factor"
representing values taken from the finite set given
by
levels
.
It is important that this object is not numeric;
in particular, comparisons and other operations behave as if
they operated on values from the levels set,
which is always of mode character.
NA
s can appear,
indicating that the corresponding value is undefined.
The expression
na.include(f)
returns
a factor like
f
,
but with
NA
s made into a level.
is.factor
returns
TRUE
if
x
is a factor. Otherwise, it returns
FALSE
.
as.factor
returns
x
,
if
x
is a factor. Otherwise, it returns
factor(x)
.
To convert a
bdFactor
to factor, you must call
bd.coerce
.
occupation <- c("doctor", "lawyer", "mechanic", "engineer") income <- c(150000, 100000, 30000, 60000) factor(occupation) factor(cut(income, breaks=c(0, 30000, 70000, 200000)), labels=c("low", "mid", "high")) # Make readable labels: occ <- factor(occupation, level = c("d", "l", "m", "e"), label=c("Doctor", "Lawyer", "Mechanic", "Engineer")) color <- c("red", "red", "red", "green", "blue") colors <- factor(color, c("red", "green", "blue")) table(colors) # table counting occurrences of colors # Treat word "Unknown" as a missing value flag: colors <- factor(c("red", "green", "Unknown", "blue"), exclude="Unknown") is.na(colors) # 3rd value will be T, the rest F