Description Usage Arguments Details Value Examples
Function to recode values within columns of a dataset using a custom codebook
1 2 3 4 5 6 7 8 |
data |
a |
codebook |
a |
variable |
a character string containing the column name
within |
from |
a character string containing the column name
within |
to |
a character string containing the column name
within |
factor_levels |
a character string containing the column name
within |
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.
A data.frame
similar to data
, except that
specified values will have been recoded based on codebook
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.