Description Usage Arguments Details Value Examples
View source: R/technical_funs.R
Easily rename one or more of the existing levels of fac
1 | rename_levels(fac, old, new)
|
fac |
Factor for which levels should be changed |
old |
Character vector of old levels to rename |
new |
Character vector of new names to replace those in 'old'. |
'new' should be as long as 'old' or of length 1. If it is as long as 'old', then the first value in 'new' will replace the first value in 'old', etc. if it is of length 1, then all levels in 'old' will be replace by 'new'.
This works as expected even if a level in 'old' is not present in fac or if a level in 'new' is already present in fac.
'fac' with modified levels.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # creating a factor
fac <- factor(c("PC", "laptop", "mobile", "telephone", "TV"))
# 1 replacement
rename_levels(fac, old="PC", new="computer")
# 1 replacement to already existing level
rename_levels(fac, old="telephone", new="mobile")
# 2 parallel replacements
rename_levels(fac, old=c("PC", "TV"), new=c("computer", "television"))
# 2 replacement to one common level
rename_levels(fac, old=c("PC", "laptop"), new=c("computer"))
# Replacement of inexisting level
rename_levels(fac, old="minitel", new="oldie")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.