| <!DOCTYPE html> |
| <!-- Generated by pkgdown: do not edit by hand --><html lang="en-US"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <meta charset="utf-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> |
| <title>Metadata • Arrow R Package</title> |
| <!-- favicons --><link rel="icon" type="image/png" sizes="96x96" href="../favicon-96x96.png"> |
| <link rel="icon" type="”image/svg+xml”" href="../favicon.svg"> |
| <link rel="apple-touch-icon" sizes="180x180" href="../apple-touch-icon.png"> |
| <link rel="icon" sizes="any" href="../favicon.ico"> |
| <link rel="manifest" href="../site.webmanifest"> |
| <script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> |
| <link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet"> |
| <script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/font-awesome-6.5.2/css/all.min.css" rel="stylesheet"> |
| <link href="../deps/font-awesome-6.5.2/css/v4-shims.min.css" rel="stylesheet"> |
| <script src="../deps/headroom-0.11.0/headroom.min.js"></script><script src="../deps/headroom-0.11.0/jQuery.headroom.min.js"></script><script src="../deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js"></script><script src="../deps/clipboard.js-2.0.11/clipboard.min.js"></script><script src="../deps/search-1.0.0/autocomplete.jquery.min.js"></script><script src="../deps/search-1.0.0/fuse.min.js"></script><script src="../deps/search-1.0.0/mark.min.js"></script><!-- pkgdown --><script src="../pkgdown.js"></script><link href="../extra.css" rel="stylesheet"> |
| <meta property="og:title" content="Metadata"> |
| <meta name="description" content="Learn how Arrow uses Schemas to document structure of data objects, and how R metadata are supported in Arrow |
| "> |
| <meta property="og:description" content="Learn how Arrow uses Schemas to document structure of data objects, and how R metadata are supported in Arrow |
| "> |
| <meta property="og:image" content="https://arrow.apache.org/img/arrow-logo_horizontal_black-txt_white-bg.png"> |
| <meta property="og:image:alt" content="Apache Arrow logo, displaying the triple chevron image adjacent to the text"> |
| <!-- Matomo --><script> |
| var _paq = window._paq = window._paq || []; |
| /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ |
| /* We explicitly disable cookie tracking to avoid privacy issues */ |
| _paq.push(['disableCookies']); |
| _paq.push(['trackPageView']); |
| _paq.push(['enableLinkTracking']); |
| (function() { |
| var u="https://analytics.apache.org/"; |
| _paq.push(['setTrackerUrl', u+'matomo.php']); |
| _paq.push(['setSiteId', '20']); |
| var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
| g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); |
| })(); |
| </script><!-- End Matomo Code --><!-- Kapa AI --><script async src="https://widget.kapa.ai/kapa-widget.bundle.js" data-website-id="9db461d5-ac77-4b3f-a5c5-75efa78339d2" data-project-name="Apache Arrow" data-project-color="#000000" data-project-logo="https://arrow.apache.org/img/arrow-logo_chevrons_white-txt_black-bg.png" data-modal-disclaimer="This is a custom LLM with access to all of [Arrow documentation](https://arrow.apache.org/docs/). If you want an R-specific answer, please mention this in your question." data-consent-required="true" data-user-analytics-cookie-enabled="false" data-consent-screen-disclaimer="By clicking "I agree, let's chat", you consent to the use of the AI assistant in accordance with kapa.ai's [Privacy Policy](https://www.kapa.ai/content/privacy-policy). This service uses reCAPTCHA, which requires your consent to Google's [Privacy Policy](https://policies.google.com/privacy) and [Terms of Service](https://policies.google.com/terms). By proceeding, you explicitly agree to both kapa.ai's and Google's privacy policies."></script><!-- End Kapa AI --> |
| </head> |
| <body> |
| <a href="#main" class="visually-hidden-focusable">Skip to contents</a> |
| |
| |
| <nav class="navbar fixed-top navbar-dark navbar-expand-lg bg-black"><div class="container"> |
| |
| <a class="navbar-brand me-2" href="../index.html">Arrow R Package</a> |
| |
| <span class="version"> |
| <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">21.0.0.9000</small> |
| </span> |
| |
| |
| <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation"> |
| <span class="navbar-toggler-icon"></span> |
| </button> |
| |
| <div id="navbar" class="collapse navbar-collapse ms-3"> |
| <ul class="navbar-nav me-auto"> |
| <li class="nav-item"><a class="nav-link" href="../articles/arrow.html">Get started</a></li> |
| <li class="nav-item"><a class="nav-link" href="../reference/index.html">Reference</a></li> |
| <li class="active nav-item dropdown"> |
| <button class="nav-link dropdown-toggle" type="button" id="dropdown-articles" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Articles</button> |
| <ul class="dropdown-menu" aria-labelledby="dropdown-articles"> |
| <li><hr class="dropdown-divider"></li> |
| <li><h6 class="dropdown-header" data-toc-skip>Using the package</h6></li> |
| <li><a class="dropdown-item" href="../articles/read_write.html">Reading and writing data files</a></li> |
| <li><a class="dropdown-item" href="../articles/data_wrangling.html">Data analysis with dplyr syntax</a></li> |
| <li><a class="dropdown-item" href="../articles/dataset.html">Working with multi-file data sets</a></li> |
| <li><a class="dropdown-item" href="../articles/python.html">Integrating Arrow, Python, and R</a></li> |
| <li><a class="dropdown-item" href="../articles/fs.html">Using cloud storage (S3, GCS)</a></li> |
| <li><a class="dropdown-item" href="../articles/flight.html">Connecting to a Flight server</a></li> |
| <li><hr class="dropdown-divider"></li> |
| <li><h6 class="dropdown-header" data-toc-skip>Arrow concepts</h6></li> |
| <li><a class="dropdown-item" href="../articles/data_objects.html">Data objects</a></li> |
| <li><a class="dropdown-item" href="../articles/data_types.html">Data types</a></li> |
| <li><a class="dropdown-item" href="../articles/metadata.html">Metadata</a></li> |
| <li><hr class="dropdown-divider"></li> |
| <li><h6 class="dropdown-header" data-toc-skip>Installation</h6></li> |
| <li><a class="dropdown-item" href="../articles/install.html">Installing on Linux</a></li> |
| <li><a class="dropdown-item" href="../articles/install_nightly.html">Installing development versions</a></li> |
| <li><hr class="dropdown-divider"></li> |
| <li><a class="dropdown-item" href="../articles/index.html">More articles...</a></li> |
| </ul> |
| </li> |
| <li class="nav-item"><a class="nav-link" href="../news/index.html">Changelog</a></li> |
| </ul> |
| <form class="form-inline my-2 my-lg-0" role="search"> |
| <input type="search" class="form-control me-sm-2" aria-label="Toggle navigation" name="search-input" data-search-index="../search.json" id="search-input" placeholder="" autocomplete="off"> |
| </form> |
| |
| <ul class="navbar-nav"> |
| <li class="nav-item"><a class="external-link nav-link" href="https://github.com/apache/arrow/" aria-label="GitHub"><span class="fa fab fa-github fa-lg"></span></a></li> |
| </ul> |
| </div> |
| |
| |
| </div> |
| </nav><div class="container template-article"> |
| |
| |
| |
| |
| <div class="row"> |
| <main id="main" class="col-md-9"><div class="page-header"> |
| |
| <h1>Metadata</h1> |
| |
| |
| <small class="dont-index">Source: <a href="https://github.com/apache/arrow/blob/main/r/vignettes/metadata.Rmd" class="external-link"><code>vignettes/metadata.Rmd</code></a></small> |
| <div class="d-none name"><code>metadata.Rmd</code></div> |
| </div> |
| |
| |
| |
| <p>This article describes the various data and metadata object types |
| supplied by arrow, and documents how these objects are structured.</p> |
| <div class="section level2"> |
| <h2 id="arrow-metadata-classes">Arrow metadata classes<a class="anchor" aria-label="anchor" href="#arrow-metadata-classes"></a> |
| </h2> |
| <p>The arrow package defines the following classes for representing |
| metadata:</p> |
| <ul> |
| <li>A <code>Schema</code> is a list of <code>Field</code> objects used |
| to describe the structure of a tabular data object; where</li> |
| <li>A <code>Field</code> specifies a character string name and a |
| <code>DataType</code>; and</li> |
| <li>A <code>DataType</code> is an attribute controlling how values are |
| represented</li> |
| </ul> |
| <p>Consider this:</p> |
| <div class="sourceCode" id="cb1"><pre class="downlit sourceCode r"> |
| <code class="sourceCode R"><span><span class="va">df</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">3</span>, y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span><span class="op">)</span><span class="op">)</span></span> |
| <span><span class="va">tb</span> <span class="op"><-</span> <span class="fu"><a href="../reference/table.html">arrow_table</a></span><span class="op">(</span><span class="va">df</span><span class="op">)</span></span> |
| <span><span class="va">tb</span><span class="op">$</span><span class="va">schema</span></span></code></pre></div> |
| <pre><code><span><span class="co">## Schema</span></span> |
| <span><span class="co">## x: int32</span></span> |
| <span><span class="co">## y: string</span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## See $metadata for additional Schema metadata</span></span></code></pre> |
| <p>The schema that has been automatically inferred could also be |
| manually created:</p> |
| <div class="sourceCode" id="cb3"><pre class="downlit sourceCode r"> |
| <code class="sourceCode R"><span><span class="fu"><a href="../reference/schema.html">schema</a></span><span class="op">(</span></span> |
| <span> <span class="fu"><a href="../reference/Field.html">field</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"x"</span>, type <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">int32</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span>,</span> |
| <span> <span class="fu"><a href="../reference/Field.html">field</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"y"</span>, type <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">utf8</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span> |
| <span><span class="op">)</span></span></code></pre></div> |
| <pre><code><span><span class="co">## Schema</span></span> |
| <span><span class="co">## x: int32</span></span> |
| <span><span class="co">## y: string</span></span></code></pre> |
| <p>The <code><a href="../reference/schema.html">schema()</a></code> function allows the following shorthand to |
| define fields:</p> |
| <div class="sourceCode" id="cb5"><pre class="downlit sourceCode r"> |
| <code class="sourceCode R"><span><span class="fu"><a href="../reference/schema.html">schema</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">int32</a></span><span class="op">(</span><span class="op">)</span>, y <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">utf8</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span></code></pre></div> |
| <pre><code><span><span class="co">## Schema</span></span> |
| <span><span class="co">## x: int32</span></span> |
| <span><span class="co">## y: string</span></span></code></pre> |
| <p>Sometimes it is important to specify the schema manually, |
| particularly if you want fine-grained control over the Arrow data |
| types:</p> |
| <div class="sourceCode" id="cb7"><pre class="downlit sourceCode r"> |
| <code class="sourceCode R"><span><span class="fu"><a href="../reference/table.html">arrow_table</a></span><span class="op">(</span><span class="va">df</span>, schema <span class="op">=</span> <span class="fu"><a href="../reference/schema.html">schema</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">int64</a></span><span class="op">(</span><span class="op">)</span>, y <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">utf8</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span></code></pre></div> |
| <pre><code><span><span class="co">## Table</span></span> |
| <span><span class="co">## 3 rows x 2 columns</span></span> |
| <span><span class="co">## $x <int64></span></span> |
| <span><span class="co">## $y <string></span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## See $metadata for additional Schema metadata</span></span></code></pre> |
| <div class="sourceCode" id="cb9"><pre class="downlit sourceCode r"> |
| <code class="sourceCode R"><span><span class="fu"><a href="../reference/table.html">arrow_table</a></span><span class="op">(</span><span class="va">df</span>, schema <span class="op">=</span> <span class="fu"><a href="../reference/schema.html">schema</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">float64</a></span><span class="op">(</span><span class="op">)</span>, y <span class="op">=</span> <span class="fu"><a href="../reference/data-type.html">utf8</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span></code></pre></div> |
| <pre><code><span><span class="co">## Table</span></span> |
| <span><span class="co">## 3 rows x 2 columns</span></span> |
| <span><span class="co">## $x <double></span></span> |
| <span><span class="co">## $y <string></span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## See $metadata for additional Schema metadata</span></span></code></pre> |
| </div> |
| <div class="section level2"> |
| <h2 id="r-object-attributes">R object attributes<a class="anchor" aria-label="anchor" href="#r-object-attributes"></a> |
| </h2> |
| <p>Arrow supports custom key-value metadata attached to Schemas. When we |
| convert a <code>data.frame</code> to an Arrow Table or RecordBatch, the |
| package stores any <code><a href="https://rdrr.io/r/base/attributes.html" class="external-link">attributes()</a></code> attached to the columns of |
| the <code>data.frame</code> in the Arrow object Schema. Attributes added |
| to objects in this fashion are stored under the <code>r</code> key, as |
| shown below:</p> |
| <div class="sourceCode" id="cb11"><pre class="downlit sourceCode r"> |
| <code class="sourceCode R"><span><span class="co"># data frame with custom metadata</span></span> |
| <span><span class="va">df</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">3</span>, y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span><span class="op">)</span><span class="op">)</span></span> |
| <span><span class="fu"><a href="https://rdrr.io/r/base/attr.html" class="external-link">attr</a></span><span class="op">(</span><span class="va">df</span>, <span class="st">"df_meta"</span><span class="op">)</span> <span class="op"><-</span> <span class="st">"custom data frame metadata"</span></span> |
| <span><span class="fu"><a href="https://rdrr.io/r/base/attr.html" class="external-link">attr</a></span><span class="op">(</span><span class="va">df</span><span class="op">$</span><span class="va">y</span>, <span class="st">"col_meta"</span><span class="op">)</span> <span class="op"><-</span> <span class="st">"custom column metadata"</span></span> |
| <span></span> |
| <span><span class="co"># when converted to a Table, the metadata is preserved</span></span> |
| <span><span class="va">tb</span> <span class="op"><-</span> <span class="fu"><a href="../reference/table.html">arrow_table</a></span><span class="op">(</span><span class="va">df</span><span class="op">)</span></span> |
| <span><span class="va">tb</span><span class="op">$</span><span class="va">metadata</span></span></code></pre></div> |
| <pre><code><span><span class="co">## $r</span></span> |
| <span><span class="co">## $r$attributes</span></span> |
| <span><span class="co">## $r$attributes$df_meta</span></span> |
| <span><span class="co">## [1] "custom data frame metadata"</span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## $r$columns</span></span> |
| <span><span class="co">## $r$columns$x</span></span> |
| <span><span class="co">## NULL</span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## $r$columns$y</span></span> |
| <span><span class="co">## $r$columns$y$attributes</span></span> |
| <span><span class="co">## $r$columns$y$attributes$col_meta</span></span> |
| <span><span class="co">## [1] "custom column metadata"</span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## </span></span> |
| <span><span class="co">## $r$columns$y$columns</span></span> |
| <span><span class="co">## NULL</span></span></code></pre> |
| <p>It is also possible to assign additional string metadata under any |
| other key you wish, using a command like this:</p> |
| <div class="sourceCode" id="cb13"><pre class="downlit sourceCode r"> |
| <code class="sourceCode R"><span><span class="va">tb</span><span class="op">$</span><span class="va">metadata</span><span class="op">$</span><span class="va">new_key</span> <span class="op"><-</span> <span class="st">"new value"</span></span></code></pre></div> |
| <p>Metadata attached to a Schema is preserved when writing the Table to |
| Arrow/Feather or Parquet formats. When reading those files into R, or |
| when calling <code><a href="https://rdrr.io/r/base/as.data.frame.html" class="external-link">as.data.frame()</a></code> on a Table or RecordBatch, the |
| column attributes are restored to the columns of the resulting |
| <code>data.frame</code>. This means that custom data types, including |
| <code>haven::labelled</code>, <code>vctrs</code> annotations, and |
| others, are preserved when doing a round-trip through Arrow.</p> |
| <p>Note that the attributes stored in <code>$metadata$r</code> are only |
| understood by R. If you write a <code>data.frame</code> with |
| <code>haven</code> columns to a Feather file and read that in Pandas, |
| the <code>haven</code> metadata won’t be recognized there. Similarly, |
| Pandas writes its own custom metadata, which the R package does not |
| consume. You are free, however, to define custom metadata conventions |
| for your application and assign any (string) values you want to other |
| metadata keys.</p> |
| </div> |
| <div class="section level2"> |
| <h2 id="further-reading">Further reading<a class="anchor" aria-label="anchor" href="#further-reading"></a> |
| </h2> |
| <ul> |
| <li>To learn more about arrow metadata, see the documentation for |
| <code><a href="../reference/schema.html">schema()</a></code>.</li> |
| <li>To learn more about data types, see the <a href="./data_types.html">data types article</a>.</li> |
| </ul> |
| </div> |
| </main><aside class="col-md-3"><nav id="toc" aria-label="Table of contents"><h2>On this page</h2> |
| </nav></aside> |
| </div> |
| |
| |
| |
| <footer><div class="pkgdown-footer-left"> |
| <p><a href="https://arrow.apache.org/docs/r/versions.html">Older versions of these docs</a></p> |
| </div> |
| |
| <div class="pkgdown-footer-right"> |
| <p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.3.</p> |
| </div> |
| |
| </footer> |
| </div> |
| |
| |
| |
| |
| |
| </body> |
| </html> |