使用避难所导入Stata数据后访问变量标签的便捷方式

Hei*_*erg 11 label r stata r-haven

在R中,一些包(例如haven)将label属性插入变量(例如haven),这解释了变量的实质名称.例如,gdppc可能有标签GDP per capita.

这非常有用,尤其是从Stata导入数据时.但是,我仍然很难知道如何在我的工作流程中使用它.

  1. 如何快速浏览变量和变量标签?现在我必须这样做attributes(df$var),但这一点不太方便(la names(df))

  2. 如何在地块中使用这些标签?同样,我可以attr(df$var, "label")用来访问字符串标签.但是,这似乎很麻烦.

有没有官方的方法在工作流程中使用这些标签?我当然可以编写一个包装的自定义函数attr,但是当包以label不同方式实现属性时,它可能会在将来中断.因此,理想情况下,我想要一种由haven(或其他主要包)支持的官方方式.

小智 12

来自tidyverse的purrr包的解决方案:

df %>% map_chr(~attributes(.)$label)
Run Code Online (Sandbox Code Playgroud)


shi*_*iro 6

在Stata的“变量窗口”中,在简单函数中使用sapply返回变量列表:

library(dplyr)
makeVlist <- function(dta) { 
     labels <- sapply(dta, function(x) attr(x, "label"))
      tibble(name = names(labels),
             label = labels)
}
Run Code Online (Sandbox Code Playgroud)


Tho*_*mas 4

这是rio中解决的创新之一(全面披露:我写了这个包)。基本上,它提供了各种导入变量标签的方式,包括haven的方式和foreign的方式。这是一个简单的例子:

首先制作一个可重现的示例:

> library("rio")
> export(iris, "iris.dta")
Run Code Online (Sandbox Code Playgroud)

使用foreign::read.dta()(通过rio::import())导入:

> str(import("iris.dta", haven = FALSE))
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "datalabel")= chr ""
 - attr(*, "time.stamp")= chr "15 Jan 2016 20:05"
 - attr(*, "formats")= chr  "" "" "" "" ...
 - attr(*, "types")= int  255 255 255 255 253
 - attr(*, "val.labels")= chr  "" "" "" "" ...
 - attr(*, "var.labels")= chr  "" "" "" "" ...
 - attr(*, "version")= int -7
 - attr(*, "label.table")=List of 1
  ..$ Species: Named int  1 2 3
  .. ..- attr(*, "names")= chr  "setosa" "versicolor" "virginica"
Run Code Online (Sandbox Code Playgroud)

使用其本机变量属性读取 using haven::read_dta(),因为属性存储在 data.frame 级别而不是变量级别:

> str(import("iris.dta", haven = TRUE, column.labels = TRUE))
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     :Class 'labelled'  atomic [1:150] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..- attr(*, "labels")= Named int [1:3] 1 2 3
  .. .. ..- attr(*, "names")= chr [1:3] "setosa" "versicolor" "virginica"
Run Code Online (Sandbox Code Playgroud)

阅读使用haven::read_dta()我们(rio 开发人员)发现更方便的替代方案:

> str(import("iris.dta", haven = TRUE))
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "var.labels")=List of 5
  ..$ Sepal.Length: NULL
  ..$ Sepal.Width : NULL
  ..$ Petal.Length: NULL
  ..$ Petal.Width : NULL
  ..$ Species     : NULL
 - attr(*, "label.table")=List of 5
  ..$ Sepal.Length: NULL
  ..$ Sepal.Width : NULL
  ..$ Petal.Length: NULL
  ..$ Petal.Width : NULL
  ..$ Species     : Named int  1 2 3
  .. ..- attr(*, "names")= chr  "setosa" "versicolor" "virginica"
Run Code Online (Sandbox Code Playgroud)

通过将属性移至 data.frame 级别,可以使用attr(data, "label.var")等更轻松地访问它们,而不是挖掘每个变量的属性。

注意:属性的值将为 NULL,因为我只是将本机 R 数据集写入本地文件,以便使其可重现。