rc_codebook: Recode values within a wide dataset

Description Usage Arguments Details Value Examples

View source: R/rc_codebook.R

Description

Function to recode values within columns of a dataset using a custom codebook

Usage

1
2
3
4
5
6
7
8
rc_codebook(
  data,
  codebook,
  variable = NULL,
  from = NULL,
  to = NULL,
  factor_levels = NULL
)

Arguments

data

a data.frame with columns containing unique variables

codebook

a data.frame containing at least three columns corresponding to: 1) variable (column) names within data, 2) old values within data to be replaced, and 3) new values that will be used to replace the old. Optionally contains a third column specifiying factor levels. Values within columns of codebook are linked by row.

variable

a character string containing the column name within codebook housing variable names (columns) within data to be recoded

from

a character string containing the column name within codebook housing old values from data

to

a character string containing the column name within codebook housing new values that will be used as substitutes for current values within data

factor_levels

a character string containing the column name within codebook housing values that will be used to assign the order of factor levels within recoded values of data

Details

The purpose of this function is to make recoding variables within a dataset, easier through the use of a user-generated codebook. The intent is that this codebook will be stored as a tabular data file, which may be more easily edited, and subsequently imported during for analysis. Storing the codebook in this way may also make reporting changes in variable coding into others, who may not be familiar with R. Moreover, delimited text files may be version controlled to document changes to variable coding schemes throughout the analysis.

Preliminary steps include importing the dataset as a wide-format data.frame, which will correspond to the data argument and generating a data.frame that will serve as the codebook, codebook. codebook must contain at least three columns whose column names are specified in the variable, from, and to arguments of this function. variable corresponds to a column within codebook that contains the names of variables to be edited. These variables will in turn correspond to column names within data. from will specify the name of a column in codebook that contains old values within data that may subsequently be recoded. from corresponds to a column in codebook that contains new value for each old value.

Value

A data.frame similar to data, except that specified values will have been recoded based on codebook

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
codebook <- data.frame(variable = c("x", "x", "y", "y"),
                from = c("0", "1", "0", "1"),
                to = c("no", "yes", "no", "yes"),
                stringsAsFactors = FALSE)
data <- data.frame(PID = 1:4,
                  x = c("0", "1", "1", "0"),
                  y = c("1", "1", "0", "0"),
                  stringsAsFactors = FALSE)
out <- rc_codebook(data, codebook, "variable", "from", "to")
out

graggsd/sgcodebook documentation built on April 24, 2020, 5:39 a.m.