recodeMissings | R Documentation |
A function to recode all missing values to either SPSS or Stata types, uniformly (re)using the same codes across all variables.
recodeMissings(
dataset,
to = c("SPSS", "Stata", "SAS"),
dictionary = NULL,
start = -91,
...
)
dataset |
A data frame |
to |
Software to recode missing values for |
dictionary |
A named vector, with corresponding Stata missing codes to SPSS missing values |
start |
A named vector, with corresponding Stata missing codes to SPSS missing values |
... |
Other internal arguments |
When a dictionary is not provided, it is automatically constructed from the available data and metadata, using negative numbers starting from -91 and up to 27 letters starting with "a".
If the dataset contains mixed variables with SPSS and Stata style missing values, unless otherwise specified in a dictionary it uses other codes than the existing ones.
For the SPSS type of missing values, the resulting variables are coerced to a declared labelled format.
Unlike SPSS, Stata does not allow labels for character values. Both cannot be
transported from SPSS to Stata, it is either one or another. If labels are
more important to preserve than original values (especially the information
about the missing values), the argument chartonum
replaces all character
values with suitable, non-overlapping numbers and adjusts the labels
accordingly.
If no labels are found in the metadata, the original values are preserved.
A data frame with all missing values recoded consistently.
Adrian Dusa
x <- data.frame(
A = declared(
c(1:5, -92),
labels = c(Good = 1, Bad = 5, NR = -92),
na_values = -92
),
B = labelled(
c(1:5, haven::tagged_na('a')),
labels = c(DK = haven::tagged_na('a'))
),
C = declared(
c(1, -91, 3:5, -92),
labels = c(DK = -91, NR = -92),
na_values = c(-91, -92)
)
)
xrec <- recodeMissings(x, to = "Stata")
attr(xrec, "dictionary")
dictionary <- data.frame(
old = c(-91, -92, "a"),
new = c("c", "d", "c")
)
recodeMissings(x, to = "Stata", dictionary = dictionary)
recodeMissings(x, to = "SPSS")
dictionary$new <- c(-97, -98, -97)
recodeMissings(x, to = "SPSS", dictionary = dictionary)
recodeMissings(x, to = "SPSS", start = 991)
recodeMissings(x, to = "SPSS", start = -8)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.