Graphics Reference
In-Depth Information
Discussion
A more traditional R method is to use square-bracket indexing to select the items and rename
them:
sizes
<-
c(
"small"
,
"large"
,
"large"
,
"small"
,
"medium"
)
sizes
"small" "large" "large" "small" "medium"
sizes[sizes
==
"small"
]
<-
"S"
sizes[sizes
==
"medium"
]
<-
"M"
sizes[sizes
==
"large"
]
<-
"L"
sizes
"S" "L" "L" "S" "M"
See Also
If, instead of a character vector, you have a factor with levels to rename, see
Changing the Names
Recoding a Categorical Variable to Another
Categorical Variable
Problem
You want to recode a categorical variable to another variable.
Solution
For the examples here, we'll use a subset of the
PlantGrowth
data set:
# Work on a subset of the PlantGrowth data set
pg
<-
PlantGrowth[c(
1
,
2
,
11
,
21
,
22
), ]
pg
weight group
4.17
ctrl
5.58
ctrl
4.81
trt1
6.31
trt2
5.12
trt2