【问题标题】:Convert variable label for labeled numeric variable to a new character variable将标记数字变量的变量标签转换为新的字符变量
【发布时间】:2021-07-07 03:33:02
【问题描述】:

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

【问题讨论】:

    标签: r stata r-haven


    【解决方案1】:

    一种选择是使用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)
    

    【讨论】:

    • 还有没有办法在 tidyverse 风格的变异管道中做到这一点?
    • 是的;编辑答案以通过 dplyr mutate 显示一种方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2016-11-29
    • 2023-02-21
    • 2015-11-09
    • 1970-01-01
    相关资源
    最近更新 更多