Graphics Reference
In-Depth Information
small large large
Levels: large medium small
To remove them, use
droplevels()
:
sizes
<-
droplevels(sizes)
sizes
small large large
Levels: large small
Discussion
The
droplevels()
function preserves the order of factor levels.
You can use the
except
argument to keep particular levels.
Changing the Names of Items in a Character Vector
Problem
You want to change the names of items in a character vector.
Solution
Use
revalue()
or
mapvalues()
from the plyr package:
sizes
<-
c(
"small"
,
"large"
,
"large"
,
"small"
,
"medium"
)
sizes
"small" "large" "large" "small" "medium"
# With revalue(), pass it a named vector with the mappings
sizes1
<-
revalue(sizes, c(small
=
"S"
, medium
=
"M"
, large
=
"L"
))
sizes1
"S" "L" "L" "S" "M"
# Can also use quotes -- useful if there are spaces or other strange characters
revalue(sizes, c(
"small"
=
"S"
,
"medium"
=
"M"
,
"large"
=
"L"
))
# mapvalues() lets you use two separate vectors instead of a named vector
mapvalues(sizes, c(
"small"
,
"medium"
,
"large"
), c(
"S"
,
"M"
,
"L"
))