【问题标题】:extracting Stata labels in R when some variables are missing labels当某些变量缺少标签时在R中提取Stata标签
【发布时间】:2018-08-18 19:23:22
【问题描述】:

我正在处理带有变量名和标签的大型 Stata 文件。我需要这些标签来了解每个变量是什么。

我一直在用

df[] %>% map_chr(~attributes(.)$label)

提取变量名称和相关标签。不幸的是,一些数据集的变量缺少任何标签(见下图)。

这意味着当我尝试上面的代码时,我只是得到一个错误。

Error: Result 1 is not a length 1 atomic vector

理想情况下,我有办法将所有缺失的标签都称为“NA”或什么都没有,所以我可以得到这样的输出:

#

仅在缺少值的变量根本没有标签但仍包含在内的情况下。

【问题讨论】:

    标签: r attributes label r-haven


    【解决方案1】:

    我觉得 purrr 的严格性妨碍了你在这里想要的东西。如果您只是lapply()(或purrr::map()),您将获得一个非常适合使用的列表:

    # get an example Stata dataset
    webuse::webuse("auto")
    
    # drop the label on `price`
    attr(auto$price, "label") <- NULL
    
    # get all of the labels as a list
    labels <- lapply(auto, attr, "label")
    

    这给了你:

    > str(labels)
    List of 12
     $ make        : chr "Make and Model"
     $ price       : NULL
     $ mpg         : chr "Mileage (mpg)"
     $ rep78       : chr "Repair Record 1978"
     $ headroom    : chr "Headroom (in.)"
     $ trunk       : chr "Trunk space (cu. ft.)"
     $ weight      : chr "Weight (lbs.)"
     $ length      : chr "Length (in.)"
     $ turn        : chr "Turn Circle (ft.) "
     $ displacement: chr "Displacement (cu. in.)"
     $ gear_ratio  : chr "Gear Ratio"
     $ foreign     : chr "Car type"
    

    如果您愿意为没有标签的变量排除标签,您可以unlist()

    > unlist(labels)
                        make                      mpg                    rep78                 headroom 
            "Make and Model"          "Mileage (mpg)"     "Repair Record 1978"         "Headroom (in.)" 
                       trunk                   weight                   length                     turn 
     "Trunk space (cu. ft.)"          "Weight (lbs.)"           "Length (in.)"     "Turn Circle (ft.) " 
                displacement               gear_ratio                  foreign 
    "Displacement (cu. in.)"             "Gear Ratio"               "Car type"
    

    【讨论】:

    • 谢谢,但是有这么多变量(数百个!),还有这么多带有空标签的变量(谁知道有多少!),有选择地在变量上放置标签效率不高。 @Weihuang Wong 的解决方案解决了这两个问题。
    • 删除只是为了一个可重现的示例数据集。 lapply() 行是您提取标签所需的唯一内容。
    • 我的错误——我实际上更喜欢你提供的输出格式,所以我接受了你的回答。非常感谢@Thomas!
    【解决方案2】:

    您可以只传递map,然后是map_chr,即

    library(haven)
    library(dplyr)
    library(purrr)
    
    dat <- read_dta("http://data.princeton.edu/wws509/datasets/salary.dta")
    
    attributes(dat$yr)$label <- NULL
    dat %>% map_chr(~attributes(.)$label)
    # Error: Result 3 is not a length 1 atomic vector
    
    dat %>% 
      map(~attributes(.)$label) %>%
      map_chr(~ifelse(is.null(.), NA, .))
    #                                  sx                                  rk 
    #          "Sex (coded 1 for female)"                              "Rank" 
    #                                  yr                                  dg 
    #                                  NA             "Highest degree earned" 
    #                                  yd                                  sl 
    # "Years since highest degree earned"   "Academic year salary in dollars"
    

    或等效

    dat %>%
      map(~attributes(.)) %>%
      map_chr("label", .default = NA)
    

    【讨论】:

    • 非常感谢 Weihuang Wong - 您的解决方案很棒 - 我接受 @Thomas 的回答,因为我发现输出的格式更易于阅读。谢谢你们!
    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多