changeLevels: Relevel using a levelChanges list

Description Usage Arguments Details Value Examples

View source: R/data.R

Description

Relevels the columns of a data frame, as as prescribed by a level changes list, of the form generated by generateFactorRenamer.

Usage

1
2
  changeLevels(x, levelChanges, allowMissingCols = F,
    verbose = T)

Arguments

x

a data frame

levelChanges

A list, of the form generated by generateFactorRenamer

allowMissingCols

A logical or a character vector. If it is a logical, it indicates whether to allow some columns of x not to be included in levelChanges. If TRUE, only a warning is issue if there are columns missing. If FALSE, an error is given. If it is a character vector, it contains the names of any columns that are allowed not to be included in levelChanges.

verbose

A logical, indicating whether to show progress

Details

Suppose we have a data frame x, composed of a number of factor variables (in columns). These factor variables are poorly named. For example, they may just be named by by integers. We would like to rename them so that their names are more helpful and intuitive.

To do this in a clear and repeatable manner, we will create an R list. Each component of the list is given the name of a column of the data.frame x. Each of these components is a character vector, each component of which refers to a level of the corresponding factor variable in x. Each of these component of the vector is a character, taking the default value of the current name of the factor level.

The function generateFactorRenamer can be used to create such a list. This can then be altered, and then processed using this function.

Value

The data frame x, with factor levels releveled as prescribed by levelChanges.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
dat <- esoph[, 1:3]
generateFactorRenamer(dat)

# we can then change this into the following
levelChanges <- list(
agegp = c(
  "25-34" = "Young",
  "35-44" = "Young",
  "45-54" = "Middle-aged",
  "55-64" = "Middle-aged",
  "65-74" = "Old",
  "75+" = "Old"),
alcgp = c(
  "0-39g/day" = "0-39g/day",
  "40-79" = "40-79",
  "80-119" = "80-119",
  "120+" = "120+"),
tobgp = c(
  "0-9g/day" = "Light",
  "10-19" = "Medium",
  "20-29" = "Heavy",
  "30+" = "Heavy")
)
changeLevels(dat, levelChanges)
# this return the new data frame

rjbgoudie/utils.rjbg documentation built on May 27, 2019, 9:13 a.m.