Description Usage Arguments Details Value Examples
View source: R/transfer_spss_labels.R
Takes SPSS attributes from a haven_labelled
variable and attaches them to a recoded
numeric variable, which preserves variable and value labels. This function only works
with SPSS datasets read in with the haven
package.
1 2 3 4 5 6 7 8 9 10 |
x |
A vector, or an expression that will output a vector. |
reference_df |
Reference data.frame, that is to say, a data.frame read in using
|
varname |
|
variable_label |
|
value_labels |
Must be a named |
format.spss |
|
display_width |
|
use_reference |
|
Variables in an SPSS dataset read in with haven::read_sav()
will contain
additional attributes affecting how they are displayed in SPSS, such as variable labels,
value labels, and display width. Recoding variables in R will frequently cause loss of
attributes, resulting in disappearing variable and value labels if the dataset with recoded
variables is written back to SPSS.
This function enables quick transfer of said attributes from a variable
from a reference dataset to one that is being recoded. This function also allows you to overwrite
selected attributes, for example as the result of a recode that collapses categories or
creates new ones.
The vector with SPSS attributes set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # library(dplyr)
# # do not run
# # these examples are illustrative, replace them with your own datasets
# spss_df <- read_sav("spss_df.sav")
# # An expression like this would take a recoded variable such as with dplyr::case_when(),
# # carry over the value labels, format.spss and display_width attributes,
# # and replace the variable label
# spss_df_recoded <- spss_df %>%
# mutate(new_variable = transfer_spss_labels(case_when(old_variable == 1 ~ 3,
# old_variable == 3 ~ 1),
# spss_df,
# "old_variable",
# variable_label = "Old variable reversed"))
# # check using the attributes() function
# attributes(spss_df_recoded$new_variable)
# tablena(spss_df_recoded$new_variable)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.