10

如何将元数据添加到 tibble?

我想要一个描述我的每个变量名的句子,这样我就可以打印出带有相关元数据的小标题,如果我把它交给以前没有看过数据的人,他们就能理解它。

as_tibble(iris)

# A tibble: 150 × 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl>  <fctr>
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
# ... with 140 more rows

# Sepal.length. Measured from sepal attachment to stem
# Sepal.width. Measured at the widest point
# Petal.length. Measured from petal attachment to stem
# Petal.width. Measured at widest point
# Species. Nomenclature based on Integrated Taxonomic Information System (ITIS), January 2018.

谢谢!

4

3 回答 3

11

这似乎很棘手。原则上@hrbrmstr 的注释是要走的路(即使用?comment?attr向任何对象添加属性),但默认情况下不会打印出这些属性。原子对象的属性似乎是自动打印的:

> z <- 1:6
> attr(z,"hello") <- "goodbye"
> z
[1] 1 2 3 4 5 6
attr(,"hello")
[1] "goodbye"

...但是,唉,对于数据帧或小标题:

dd <- tibble::tibble(x=1:4,y=2:5)
> attr(dd,"metadata") <- c("some stuff","some more stuff")
> dd
# A tibble: 4 x 2
      x     y
  <int> <int>
1     1     2
2     2     3
3     3     4
4     4     5

你可以用它自己的 S3 类包装对象来打印这些东西:

class(dd) <- c("my_tbl",class(dd))
> print.my_tbl <- function(x) {
+    NextMethod(x)
+    print(attr(x,"metadata"))
+    invisible(x)
+ }
> dd
# A tibble: 4 x 2
      x     y
  <int> <int>
1     1     2
2     2     3
3     3     4
4     4     5
[1] "some stuff"      "some more stuff"

您可以使打印更精致或更漂亮,例如

cat("\nMETADATA:\n")
cat(sprintf("# %s",attr(x,"metadata")),sep="\n")

如果其他用户尚未定义,则不会发生任何不良情况print.my_tbl(打印方法将回退到小标题的打印方法),但只有当他们有您的定义时才会打印元数据print.my_tbl......

于 2018-01-08T20:32:25.777 回答
6

这与 Ben Bolker 的建议并没有什么不同,但从概念上讲,如果我希望信息与数据框中的向量相关,我希望它们直接与向量相关联。换句话说,我更愿意将属性添加到向量本身而不是数据框对象。

我不知道我是否会向对象添加自定义类,但也许您可以为类似数据框的对象调用一个单独的函数就足够了:

library(tibble)
library(ggplot2)
library(magrittr)
library(labelVector)

print_with_label <- function(dframe){
  stopifnot(inherits(dframe, "data.frame"))
  labs <- labelVector::get_label(dframe, names(dframe))
  labs <- sprintf("%s: %s", names(dframe), labs)
  print(dframe)
  cat("\n")
  cat(labs, sep = "\n")
}

iris <- 
  as_tibble(iris) %>%  
  set_label(Sepal.Length = "This is a user friendly label",
            Petal.Length = "I much prefer reading human over computer")

print_with_label(iris)

mtcars <-
  set_label(mtcars,
            mpg = "Miles per Gallon",
            qsec = "Quarter mile time",
            hp = "Horsepower",
            cyl = "Cylinders",
            disp = "Engine displacement")

print_with_label(mtcars)
于 2018-01-08T20:48:19.687 回答
4

回复较晚,抱歉。但是自从我第一次开始学习 R 以来,这个话题就一直困扰着我。在我的工作中,将元数据分配给列并不常见。这是必需的。R 似乎没有一个很好的方法来做到这一点,这真的让我很困扰。如此之多,以至于我写了一些包来做到这一点。

fmtr包具有分配描述(以及其他内容)的功能并且libr包具有字典功能,因此您可以查看您分配的所有元数据。

下面是它的工作原理:

首先,将描述分配给列。您只需将命名列表发送到该descriptions()函数。

library(fmtr)
library(libr)

# Create data frame
df <- iris

# Assign descriptions
descriptions(df) <- list(Sepal.Length = "Measured from sepal attachment to stem", 
                         Sepal.Width = "Measured at the widest point",
                         Petal.Length = "Measured from petal attachment to stem", 
                         Petal.Width = "Measured at the widest point",
                         Species = paste("Nomanclature based on Integrated Taxonomic", 
                                         "Information System (ITIS), January 2018."))


然后您可以通过调用该dictionary()函数来查看所有元数据,如下所示:

dictionary(df)
# # A tibble: 5 x 10
#  Name  Column      Class  Label Description                                                 
#  <chr> <chr>       <chr>  <chr> <chr>                                                      
# 1 df    Sepal.Leng~ numer~ NA    Measured from sepal attachment to stem                     
# 2 df    Sepal.Width numer~ NA    Measured at the widest point                                
# 3 df    Petal.Leng~ numer~ NA    Measured from petal attachment to stem                      
# 4 df    Petal.Width numer~ NA    Measured at the widest point                                 
# 5 df    Species     factor NA    Nomanclature based on Integrated Taxonomic Information Syst~

如果您愿意,您可以将字典作为其自己的数据框返回,然后保存或打印或其他方式。

d <- dictionary(df)

这是字典数据框:

字典数据框

于 2021-08-06T03:52:32.400 回答