varlabTemplate: Create Variable Label Template

View source: R/variableKey.R

varlabTemplateR Documentation

Create Variable Label Template

Description

Receive a key, create a varlab object, with columns name_old name_new, and varlab.

Usage

varlabTemplate(obj, varlab = TRUE)

Arguments

obj

A variable key

varlab

Default NULL, function will start from clean slate, a set of column labels that match name_new. User can specify values by providing a named vector of labels, e.g., c("x1" = "happiness", "x2" = "wealth"), where the names are values to be matched against "name_new" in key.

Details

If not specified, a matrix is created with empty variable labels.

Value

Character matrix with columns name_new and varlab.

Author(s)

Paul Johnson

Examples

mydf.path <- system.file("extdata", "mydf.csv", package = "kutils")
mydf <- read.csv(mydf.path, stringsAsFactors=FALSE)
mydf.keywide1 <- keyTemplate(mydf, long = FALSE, sort = FALSE,
                    varlab = TRUE)
attr(mydf.keywide1, "varlab")
mydf.keywide2 <- keyTemplate(mydf, long = FALSE, sort = FALSE,
                    varlab = c("x3" = "fun"))
attr(mydf.keywide2, "varlab")
attr(mydf.keywide2, "varlab") <- varlabTemplate(mydf.keywide2,
                  varlab = c("x5" = "wealth", "x10" = "happy"))
attr(mydf.keywide2, "varlab")
attr(mydf.keywide2, "varlab") <- varlabTemplate(mydf.keywide2,
                  varlab = TRUE)
attr(mydf.keywide2, "varlab")
## Target we are trying to match:
mydf.keylong <- keyTemplate(mydf, long = TRUE, sort = FALSE, varlab = TRUE)
attr(mydf.keylong, "varlab")
attr(mydf.keylong, "varlab") <- NULL
varlabTemplate(mydf.keylong)
attr(mydf.keylong, "varlab") <- varlabTemplate(mydf.keylong,
                   varlab = c("x3" = "wealth", "x10" = "happy"))
attr(mydf.keylong, "varlab")
attr(mydf.keylong, "varlab") <- varlabTemplate(mydf.keylong, varlab = TRUE)
attr(mydf.keylong, "varlab")

kutils documentation built on Sept. 17, 2023, 5:06 p.m.