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

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.

Value

Character matrix with columns name_new and varlab.

Details

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

Author

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")
#>   x5   x4   x3   x2   x1   x7   x6 
#> "x5" "x4" "x3" "x2" "x1" "x7" "x6" 
mydf.keywide2 <- keyTemplate(mydf, long = FALSE, sort = FALSE,
                    varlab = c("x3" = "fun"))
attr(mydf.keywide2, "varlab")
#>    x3 
#> "fun" 
attr(mydf.keywide2, "varlab") <- varlabTemplate(mydf.keywide2,
                  varlab = c("x5" = "wealth", "x10" = "happy"))
attr(mydf.keywide2, "varlab")
#>       x3       x5      x10 
#>    "fun" "wealth"  "happy" 
attr(mydf.keywide2, "varlab") <- varlabTemplate(mydf.keywide2,
                  varlab = TRUE)
attr(mydf.keywide2, "varlab")
#>       x3       x5      x10       x4       x2       x1       x7       x6 
#>    "fun" "wealth"  "happy"     "x4"     "x2"     "x1"     "x7"     "x6" 
## Target we are trying to match:
mydf.keylong <- keyTemplate(mydf, long = TRUE, sort = FALSE, varlab = TRUE)
attr(mydf.keylong, "varlab")
#>   x5   x4   x3   x2   x1   x7   x6 
#> "x5" "x4" "x3" "x2" "x1" "x7" "x6" 
attr(mydf.keylong, "varlab") <- NULL
varlabTemplate(mydf.keylong)
#>   x5   x4   x3   x2   x1   x7   x6 
#> "x5" "x4" "x3" "x2" "x1" "x7" "x6" 
attr(mydf.keylong, "varlab") <- varlabTemplate(mydf.keylong,
                   varlab = c("x3" = "wealth", "x10" = "happy"))
attr(mydf.keylong, "varlab")
#>       x3      x10 
#> "wealth"  "happy" 
attr(mydf.keylong, "varlab") <- varlabTemplate(mydf.keylong, varlab = TRUE)
attr(mydf.keylong, "varlab")
#>       x3      x10       x5       x4       x2       x1       x7       x6 
#> "wealth"  "happy"     "x5"     "x4"     "x2"     "x1"     "x7"     "x6"