将带标签的数字变量的变量标签转换为新的字符变量

Convert variable label for labeled numeric variable to a new character variable

将数据集从 Stata 导入 R 时,它通常带有有用的数字变量标签。我希望能够将标签中的数据转换为新的单独变量。 Stata 中的等效命令是解码。

library(tidyverse)
library(webuse)
auto <- webuse("auto")
auto$foreign #Want to convert this to a character variable that reads "Domestic" or "Foreign"

一种选择是使用 labelled package,例如

library(tidyverse)
#install.packages("webuse")
library(webuse)
#install.packages("labelled")
library(labelled)

auto <- webuse("auto")
auto$foreign
auto$labels <- labelled::to_factor(auto$foreign, levels = "labels")
auto$labels
#>[1] Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic
#>[13] Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic
#>[25] Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic
#>[37] Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic Domestic
#>[49] Domestic Domestic Domestic Domestic Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign 
#>[61] Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign  Foreign 
#>[73] Foreign  Foreign 
#>attr(,"label")
#>[1] Car type
#>Levels: Domestic Foreign

或者,要保留值和标签:

auto$labels <- labelled::to_factor(auto$foreign, levels = "prefixed")
auto$labels
#>[1] [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic
#>[9] [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic
#>[17] [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic
#>[25] [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic
#>[33] [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic
#>[41] [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic [0] Domestic
#>[49] [0] Domestic [0] Domestic [0] Domestic [0] Domestic [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign 
#>[57] [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign 
#>[65] [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign  [1] Foreign 
#>[73] [1] Foreign  [1] Foreign 
#>attr(,"label")
#>[1] Car type
#>Levels: [0] Domestic [1] Foreign

编辑

要使用 dplyr 变异:

library(tidyverse)
#install.packages("webuse")
library(webuse)
#install.packages("labelled")
library(labelled)

auto <- webuse("auto")
auto %>% 
  mutate(labels = labelled::to_factor(auto$foreign, levels = "labels")) %>% 
  select(labels)