问题描述
将数据集从 Stata 导入 R 时,它通常带有有用的数字变量标签。我希望能够将标签中的数据转换为新的单独变量。 Stata 中的等效命令是 decode。
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 mutate:
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)