Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

wide2long

Convert a key object from wide to long 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

wide2long(key, sep = c(character = "\\|", logical = "\\|", integer =
  "\\|", factor = "\\|", ordered = "[\\|<]", numeric = "\\|"))

Arguments

key

A variable key in the wide format

sep

Default separator is the pipe, "\|" for most variables, while ordered accepts pipe or less than, "\|<". If the key did not follow those customs, other sep values may be specified for each variable class.

Value

A long format variable key

Author(s)

Paul Johnson <pauljohn@ku.edu>

Examples

mydf.path <- system.file("extdata", "mydf.csv", package = "kutils")
mydf <- read.csv(mydf.path, stringsAsFactors=FALSE)
## Target we are trying to match:
mydf.keylong <- keyTemplate(mydf, long = TRUE, sort = FALSE)

mydf.key <- keyTemplate(mydf)
mydf.keywide2long <- wide2long(mydf.key)

## rownames not meaningful in long key, so remove in both versions
row.names(mydf.keywide2long) <- NULL
row.names(mydf.keylong) <- NULL
all.equal(mydf.keylong, mydf.keywide2long)

kutils

Project Management Tools

v1.70
GPL-2
Authors
Paul Johnson [aut, cre], Benjamin Kite [aut], Charles Redmon [aut], Jared Harpole [ctb], Kenna Whitley [ctb], Po-Yi Chen [ctb], Shadi Pirhosseinloo [ctb]
Initial release
2020-04-28

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.