index_cfactor: Decode numerical into categorical data

Description Usage Arguments Details Value Examples

View source: R/index_cfactor.R

Description

Decode numerical columns in a data frame into (ordered) factors given the encoding in another data frame.

Usage

1
2
index_cfactor(data, index, variable = "variable", encoding = "encoding",
  label = "label", ...)

Arguments

data

A data frame containing at least one integer column to decode.

index

A data frame containing the names of the variable to encode, the encoding for data and labels to assign.

variable

The name of the column in index that indicates the variable.

encoding

The name of the column in index that indicates the encoding.

label

The name of the column in index that indicates the label that will be given.

...

Further arguments to be passed to or from other methods, in particular to cfactor.

Details

Arguments passed via ... to cfactor are only recycled if of length 1. Otherwise, an error is thrown. All arguments passed via ... are applied in the order of the data columns but columns not to convert are skipped (see example).

Value

The original data frame is returned whereas the variables for which an encoding was provided are turned into (ordered) factors. All other columns are returned unmodified.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
data <- data.frame(var1 = sample(x = 1:10, size = 20, replace = TRUE),
                   var2 = rep(1:2, 20),
                   var3 = sample(20),
                   var4 = 2, 
                   var5 = sample(row.names(USArrests), size = 20),
                   stringsAsFactors = FALSE)

 index <- data.frame(var = rep(paste0("var", 1:3), c(10, 2, 20)),
                   encoding = c(1:10, 1:2, 1:20),
                   label = c(letters[1:10], c("male", "female"), LETTERS[1:20]))
                   
 index_cfactor(data = data, index = index, variable = "var", ordered = c(TRUE, TRUE, FALSE))

jonmcalder/refactor documentation built on Nov. 16, 2020, 3:46 a.m.