rename_levels: Easily rename one or more of the existing levels of fac

Description Usage Arguments Details Value Examples

View source: R/technical_funs.R

Description

Easily rename one or more of the existing levels of fac

Usage

1
rename_levels(fac, old, new)

Arguments

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'.

Details

'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.

Value

'fac' with modified levels.

Examples

 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")

antoine-sachet/customFunctions documentation built on Sept. 25, 2020, 8:56 p.m.