I would like to use the attributes of the dataset to recode my spss data while preserving the metadata/label attributes. The problem I am having is that anything I do removes the existing attributes while trying to recode the numeric values of my data into the text.
For example the variable AWR_A_5
below the 1's correspond to 'Kelley Blue Book'
testfile <- haven::read_sav("TestFile.sav")
> savfile$AWR_A_5
<labelled<double>[10]>: Which of the following automotive resource sites, if any,
have you heard of? Please select all that apply. Kelley Blue Book
[1] NA NA 0 NA NA 1 1 NA 1 0
Labels:
value label
1 Kelley Blue Book
So I would like to recode those 1's in the data to Kelley Blue Book while retaining the attributes. So it would look like the following. I understand this would change the column type from numeric to character.
> savfile$AWR_A_5
<labelled<character>[10]>: Which of the following automotive resource sites, if any,
have you heard of? Please select all that apply. Kelley Blue Book
[1] NA NA 0 NA NA Kelley Blue Book Kelley Blue Book NA Kelley Blue Book 0
Labels:
value label
1 Kelley Blue Book
One option to achieve your desired result would be to convert both the values or the data type and the labels to characters (as I deal regularly with such transformations I wrapped this code inside a function).
Using some fake random example data:
library(haven)
set.seed(123)
savfile <- data.frame(
AWR_A_5 = sample(c(0, 1, NA), 10, replace = TRUE)
)
savfile$AWR_A_5 <- labelled(
savfile$AWR_A_5,
label = "Which of the following automotive resource sites, if any, have you heard of? Please select all that apply. Kelley Blue Book",
labels = c("Kelley Blue Book" = 1)
)
savfile$AWR_A_5
#> <labelled<double>[10]>: Which of the following automotive resource sites, if any, have you heard of? Please select all that apply. Kelley Blue Book
#> [1] NA NA NA 1 NA 1 1 1 NA 0
#>
#> Labels:
#> value label
#> 1 Kelley Blue Book
x <- savfile$AWR_A_5
label <- attr(x, "label")
labels <- attr(x, "labels")
labels <- setNames(as.character(labels), names(labels))
labelled(
as.character(haven::as_factor(x)),
label = label,
labels = labels
)
#> <labelled<character>[10]>: Which of the following automotive resource sites, if any, have you heard of? Please select all that apply. Kelley Blue Book
#> [1] NA NA NA Kelley Blue Book
#> [5] NA Kelley Blue Book Kelley Blue Book Kelley Blue Book
#> [9] NA 0
#>
#> Labels:
#> value label
#> 1 Kelley Blue Book