Adr*_*e B 8 metadata r tidyr tibble
如何将元数据添加到 tibble 中?
我想要一个句子来描述我的每个变量名称,以便我可以打印出带有相关元数据的 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.
Run Code Online (Sandbox Code Playgroud)
谢谢!
Dav*_*sak 11
回复较晚,抱歉。但自从我第一次开始学习 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."))
Run Code Online (Sandbox Code Playgroud)
然后您可以通过调用该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~
Run Code Online (Sandbox Code Playgroud)
如果您愿意,您可以将字典作为其自己的数据框返回,然后保存或打印它或执行其他操作。
d <- dictionary(df)
Run Code Online (Sandbox Code Playgroud)
这是字典数据框:
这看起来很棘手。原则上@hrbrmstr 的注释是要走的路(即使用?comment或?attr向任何对象添加属性),但默认情况下不会打印出这些属性。原子对象的属性似乎是自动打印的:
> z <- 1:6
> attr(z,"hello") <- "goodbye"
> z
[1] 1 2 3 4 5 6
attr(,"hello")
[1] "goodbye"
Run Code Online (Sandbox Code Playgroud)
...但不是,唉,对于数据框或小标题:
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
Run Code Online (Sandbox Code Playgroud)
你可以用它自己的 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"
Run Code Online (Sandbox Code Playgroud)
你可以让印刷更精致或更漂亮,例如
cat("\nMETADATA:\n")
cat(sprintf("# %s",attr(x,"metadata")),sep="\n")
Run Code Online (Sandbox Code Playgroud)
如果其他用户未定义,则不会发生任何不好的事情print.my_tbl(打印方法将回退到小标题的打印方法),但只有在具有您的print.my_tbl定义时才会打印元数据......
这与 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1531 次 |
| 最近记录: |