keyApply: Apply variable key to data frame (generate recoded data...

View source: R/variableKey.R

keyApplyR Documentation

Apply variable key to data frame (generate recoded data frame)

Description

This is the main objective of the variable key system.

Usage

keyApply(
  dframe,
  key,
  diagnostic = TRUE,
  safeNumericToInteger = TRUE,
  trimws = "both",
  ignoreCase = TRUE,
  drop = TRUE,
  debug = FALSE
)

Arguments

dframe

An R data frame

key

A variable key object, of class either "key" or "keylong"

diagnostic

Default TRUE: Compare the old and new data frames carefully with the keyDiagnostic function.

safeNumericToInteger

Default TRUE: Should we treat values which appear to be integers as integers? If a column is numeric, it might be safe to treat it as an integer. In many csv data sets, the values coded c(1, 2, 3) are really integers, not floats c(1.0, 2.0, 3.0). See safeInteger.

trimws

Default is "both", can change to "left", "right", or set as NULL to avoid any trimming.

ignoreCase

Default TRUE. If column name is capitalized differently than name_old in the key, but the two are otherwise identical, then the difference in capitalization will be ignored.

drop

Default TRUE. True implies drop = c("vars", "vals"). TRUE applies to both variables ("vars") and values ("vals"). "vars" means that a column will be omitted from data if it is not in the key "name_old". Similarly, if anything except "." appears in value_old, then setting drop="vals" means omission of a value from key "value_old" causes observations with those values to become NA. This is the original variable key behavior. The drop argument allows "partial keys", beginning with kutils version 1.12. drop = FALSE means that neither values nor variables are omitted. Rather than TRUE, one can specify either drop = "vars", or drop = "vals".

debug

Default FALSE. If TRUE, emit some warnings.

Value

A new data.frame object, with renamed and recoded variables

Author(s)

Paul Johnson <pauljohn@ku.edu>

Examples

mydf.key.path <- system.file("extdata", "mydf.key.csv", package = "kutils")
mydf.key <-  keyImport(mydf.key.path)
mydf.path <- system.file("extdata", "mydf.csv", package = "kutils")

mydf <- read.csv(mydf.path, stringsAsFactors = FALSE)
mydf2 <- keyApply(mydf, mydf.key)

nls.keylong.path <- system.file("extdata", "natlongsurv.key_long.csv", package = "kutils")
nls.keylong <- keyImport(nls.keylong.path, long = TRUE)
data(natlongsurv)
nls.dat <- keyApply(natlongsurv, nls.keylong)


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