将值标签分配为 R 中的字符串值

Assign value labels as string values in R

你好,我在 R 中从 SPSS 导入了一个数据集,该数据集有标签,我想使用值标签作为字符串值。有办法吗?

head(dataset$A7B1)
<Labelled double>: A7b1. Cantón de San José en que reside
[1] NA NA NA 2  8 NA 4 NA 5

Labels:
 value         label
     1      SAN JOSÉ
     2        ESCAZÚ
     3  DESAMPARADOS
     4      PURISCAL
     5       TARRAZÚ
     6        ASERRÍ
     7          MORA
     8    GOICOECHEA
     9     SANTA ANA
    10    ALAJUELITA
    11      CORONADO
    12        ACOSTA
    13         TIBAS
    14       MORAVIA
    15 MONTES DE OCA
    16    TURRUBARES
    17          DOTA
    18    CURRIDABAT
    19 PÉREZ ZELEDÓN
    20   LEÓN CORTÉS

我需要每个双标值根据值标签变成一个字符串值。

glimpse(dataset)
Rows: 283
Columns: 9
$ A7A  <dbl+lbl> 2, 8, 3, 3, 1, 2, 4, 4, 4, 2, 2, 4, 3, 4, 2, 3, 1, 2, 2, 6, 1, 1, 2, 2, 1, 2, 3, 1, 2, 1, 1, 4, 3, 1, 2, 2, 1, 1, 4, ...
$ A7B1 <dbl+lbl> NA, NA, NA, NA, 8, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3, NA, NA, NA, 1, 11, NA, NA, 8, NA, NA, 3, NA, 14, 1,...
$ A7B2 <dbl+lbl> 1, NA, NA, NA, NA, 1, NA, NA, NA, 1, 1, NA, NA, NA, 1, NA, NA, 6, 2, NA, NA, NA, 1, 10, NA, 1, NA, NA, 1, NA, NA, NA,...
$ A7B3 <dbl+lbl> NA, NA, 1, 7, NA, NA, NA, NA, NA, NA, NA, NA, 3, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA...
$ A7B4 <dbl+lbl> NA, NA, NA, NA, NA, NA, 2, 1, 1, NA, NA, 9, NA, 7, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
$ A7B5 <dbl+lbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
$ A7B6 <dbl+lbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
$ A7B7 <dbl+lbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
$ A7B8 <dbl+lbl> NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA..


 dput(head(dataset$A7A))
structure(c(2, 8, 3, 3, 1, 2), label = "A7a. Provincia de residencia", labels = c(`San Jose` = 1,     Alajuela = 2, Cartago = 3, Heredia = 4, Guanacaste = 5, Puntarenas = 6, 
Limon = 7, Extrenjero = 8), class = "haven_labelled")

我通常在读取 SPSS 数据时使用 haven 并为此提供辅助函数。希望这有帮助——如果没有,请在您的问题中提供更多信息:)

library(haven)

swap_labels <- function(x, keep_original = TRUE) {
  
  labels <- attr(x, "labels")
  new_vec <- names(labels)[match(x, labels)]
  
  if(keep_original) {
    haven::labelled_spss(new_vec, setNames(names(labels), labels))
  } else {
    new_vec
  }
  
}

# Reproducible example
test_vec <- labelled_spss(1:3, labels = setNames(1:3, letters[1:3]))

> test_vec
<labelled_spss<integer>[3]>
[1] 1 2 3

Labels:
 value label
     1     a
     2     b
     3     c

> swap_labels(test_vec)
<labelled_spss<character>[3]>
[1] a b c

Labels:
 value label
     a     1
     b     2
     c     3