微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将标记数字变量的变量标签转换为新的字符变量

将数据集从 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。