Learn R Programming

kutils (version 0.93)

long2wide: convert a key object from long to wide format

Description

##' This is not flexible, assumes columns are named in our canonical style, which means the columns are named c("name_old", "name_new", "class_old", "class_new", "value_old", "value_new").

Usage

long2wide(keylong)

Arguments

keylong
A variable key in the long format

Value

A wide format variable key

Examples

Run this code
mydf.path <- system.file("extdata", "mydf.csv", package = "kutils")
mydf <- read.csv(mydf.path, stringsAsFactors=FALSE)
## A wide key we are trying to match:
mydf.key <- keyTemplate(mydf, long = FALSE, sort = TRUE)
mydf.key["x4", "missings"] <- "c(999)"
## A long ke we will convert next
mydf.keylong <- keyTemplate(mydf, long = TRUE, sort = TRUE)
mydf.keylong["11", "missings"] <- "c(999)"
mydf.long2wide <- long2wide(mydf.keylong)
## Tune the rownames to match style of long key
rownames(mydf.key) <- paste0(mydf.key$name_old, ".", mydf.key$name_new)
all.equal(mydf.key, mydf.long2wide)

Run the code above in your browser using DataLab