{"id":1136,"date":"2019-11-08T17:39:46","date_gmt":"2019-11-09T00:39:46","guid":{"rendered":"http:\/\/www.zhuoyao.net\/?p=1136"},"modified":"2022-11-22T06:56:36","modified_gmt":"2022-11-22T06:56:36","slug":"tibble","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2019\/11\/08\/tibble\/","title":{"rendered":"tibble"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Coerce lists, matrices, and more to data frames<\/h1>\n\n\n\n<p>\n    Source: <a href=\"https:\/\/github.com\/tidyverse\/tibble\/blob\/master\/R\/as_tibble.R\"><code>R\/as_tibble.R<\/code><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.tidyverse.org\/lifecycle\/#maturing\"><img decoding=\"async\" src=\"https:\/\/tibble.tidyverse.org\/reference\/figures\/lifecycle-maturing.svg\" alt=\"Maturing lifecycle\"\/><\/a><\/figure>\n\n\n\n<p><code>as_tibble()<\/code> turns an existing object, such as a data frame, list, or\nmatrix, into a so-called tibble, a data frame with class <code><a href=\"https:\/\/tibble.tidyverse.org\/reference\/tbl_df-class.html\">tbl_df<\/a><\/code>. This is\nin contrast with <code><a href=\"https:\/\/tibble.tidyverse.org\/reference\/tibble.html\">tibble()<\/a><\/code>, which builds a tibble from individual columns.\n<code>as_tibble()<\/code> is to <code><a href=\"https:\/\/tibble.tidyverse.org\/reference\/tibble.html\">tibble()<\/a><\/code> as <code><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/as.data.frame\">base::as.data.frame()<\/a><\/code> is to\n<code><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/data.frame\">base::data.frame()<\/a><\/code>.<\/p>\n\n\n\n<p><code>as_tibble()<\/code> is an S3 generic, with methods for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/data.frame\">data.frame<\/a><\/code>: Thin wrapper around the <code>list<\/code> method\nthat implements tibble&#8217;s treatment of <a href=\"https:\/\/tibble.tidyverse.org\/reference\/rownames.html\">rownames<\/a>.<\/li>\n\n\n\n<li>list<\/li>\n\n\n\n<li><code><a href=\"https:\/\/www.rdocumentation.org\/packages\/methods\/topics\/StructureClasses\">matrix<\/a><\/code>, <code><a href=\"https:\/\/www.rdocumentation.org\/packages\/stats\/topics\/poly\">poly<\/a><\/code>,\n<code><a href=\"https:\/\/www.rdocumentation.org\/packages\/stats\/topics\/ts\">ts<\/a><\/code>, <code><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/table\">table<\/a><\/code><\/li>\n\n\n\n<li>Default: An atomic vector is first coerced to a list and, unlike\n<code><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/as.data.frame\">base::as.data.frame()<\/a><\/code>, the returned tibble has one column per element.\nOther inputs are first coerced with <code><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/as.data.frame\">base::as.data.frame()<\/a><\/code>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">as_tibble(x, ..., .rows = NULL, .name_repair = <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/c\">c<\/a>(\"check_unique\",\n  \"unique\", \"universal\", \"minimal\"),\n  rownames = pkgconfig::<a href=\"https:\/\/www.rdocumentation.org\/packages\/pkgconfig\/topics\/get_config\">get_config<\/a>(\"tibble::rownames\", NULL))\n\n# S3 method for data.frame\nas_tibble(x, validate = NULL, ..., .rows = NULL,\n  .name_repair = <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/c\">c<\/a>(\"check_unique\", \"unique\", \"universal\", \"minimal\"),\n  rownames = pkgconfig::<a href=\"https:\/\/www.rdocumentation.org\/packages\/pkgconfig\/topics\/get_config\">get_config<\/a>(\"tibble::rownames\", NULL))\n\n# S3 method for list\nas_tibble(x, validate = NULL, ..., .rows = NULL,\n  .name_repair = <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/c\">c<\/a>(\"check_unique\", \"unique\", \"universal\", \"minimal\"))\n\n# S3 method for matrix\nas_tibble(x, ..., validate = NULL,\n  .name_repair = NULL)\n\n# S3 method for table\nas_tibble(x, `_n` = \"n\", ..., n = `_n`)\n\n# S3 method for NULL\nas_tibble(x, ...)\n\n# S3 method for default\nas_tibble(x, ...)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"arguments\">Arguments<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>x<\/th><td>A data frame, list, matrix, or other object that could reasonably be\ncoerced to a tibble.<\/td><\/tr><tr><th>&#8230;<\/th><td>Other arguments passed on to individual methods.<\/td><\/tr><tr><th>.rows<\/th><td>The number of rows, useful to create a 0-column tibble or\njust as an additional check.<\/td><\/tr><tr><th>.name_repair<\/th><td>Treatment of problematic column names:\n<code>\"minimal\"<\/code>: No name repair or checks, beyond basic existence,\n<code>\"unique\"<\/code>: Make sure names are unique and not empty,\n<code>\"check_unique\"<\/code>: (default value), no name repair, but check they are\n<code>unique<\/code>,\n<code>\"universal\"<\/code>: Make the names <code>unique<\/code> and syntactic\na function: apply custom name repair (e.g., <code>.name_repair = make.names<\/code>\nfor names in the style of base R).\nA purrr-style anonymous function, see <code><a href=\"https:\/\/rlang.r-lib.org\/reference\/as_function.html\">rlang::as_function()<\/a><\/code>\n\n\nSee <a href=\"https:\/\/tibble.tidyverse.org\/reference\/name-repair.html\">name-repair<\/a> for more details on these terms and the strategies used\nto enforce them.<\/td><\/tr><tr><th>rownames<\/th><td>How to treat existing row names of a data frame or matrix:\n<code>NULL<\/code>: remove row names. This is the default.\n<code>NA<\/code>: keep row names.\nA string: the name of a new column. Existing rownames are transferred\ninto this column and the <code>row.names<\/code> attribute is deleted.\nRead more in <a href=\"https:\/\/tibble.tidyverse.org\/reference\/rownames.html\">rownames<\/a>.\n<\/td><\/tr><tr><th>_n, validate<\/th><td>For compatibility only, do not use for new code.<\/td><\/tr><tr><th>n<\/th><td>Name for count column, default: <code>\"n\"<\/code>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"row-names\">Row names<\/h2>\n\n\n\n<p>The default behavior is to silently remove row names.<\/p>\n\n\n\n<p>New code should explicitly convert row names to a new column using the\n<code>rownames<\/code> argument.<\/p>\n\n\n\n<p>For existing code that relies on the retention of row names, call\n<code><a href=\"https:\/\/www.rdocumentation.org\/packages\/pkgconfig\/topics\/set_config\">pkgconfig::set_config(\"tibble::rownames\" = NA)<\/a><\/code> in your script or in your\npackage&#8217;s <code><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/ns-hooks\">.onLoad()<\/a><\/code>  function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"see-also\">See also<\/h2>\n\n\n\n<p><code><a href=\"https:\/\/tibble.tidyverse.org\/reference\/tibble.html\">tibble()<\/a><\/code> constructs a tibble from individual columns. <code><a href=\"https:\/\/tibble.tidyverse.org\/reference\/enframe.html\">enframe()<\/a><\/code>\nconverts a named vector to a tibble with a column of names and column of\nvalues. <a href=\"https:\/\/tibble.tidyverse.org\/reference\/name-repair.html\">name-repair<\/a> documents the details of name repair.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples\">Examples<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">l &lt;- <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/list\">list<\/a>(x = 1:500, y = <a href=\"https:\/\/www.rdocumentation.org\/packages\/stats\/topics\/Uniform\">runif<\/a>(500), z = 500:1)\ndf &lt;- as_tibble(l)\n\nm &lt;- <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/matrix\">matrix<\/a>(<a href=\"https:\/\/www.rdocumentation.org\/packages\/stats\/topics\/Normal\">rnorm<\/a>(50), ncol = 5)\n<a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/colnames\">colnames<\/a>(m) &lt;- <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/c\">c<\/a>(\"a\", \"b\", \"c\", \"d\", \"e\")\ndf &lt;- as_tibble(m)\n\nas_tibble(<a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/list\">as.list<\/a>(1:3), .name_repair = \"unique\")#&gt; New names:\n#&gt; * `` -&gt; ...1\n#&gt; * `` -&gt; ...2\n#&gt; * `` -&gt; ...3#&gt; # A tibble: 1 x 3\n#&gt;    ...1  ...2  ...3\n#&gt;   &lt;int&gt; &lt;int&gt; &lt;int&gt;\n#&gt; 1     1     2     3\n# Prefer enframe() for vectors\n<a href=\"https:\/\/tibble.tidyverse.org\/reference\/enframe.html\">enframe<\/a>(1:3)#&gt; # A tibble: 3 x 2\n#&gt;    name value\n#&gt;   &lt;int&gt; &lt;int&gt;\n#&gt; 1     1     1\n#&gt; 2     2     2\n#&gt; 3     3     3<a href=\"https:\/\/tibble.tidyverse.org\/reference\/enframe.html\">enframe<\/a>(1:3, name = NULL)#&gt; # A tibble: 3 x 1\n#&gt;   value\n#&gt;   &lt;int&gt;\n#&gt; 1     1\n#&gt; 2     2\n#&gt; 3     3\n# For list-like inputs, `as_tibble()` is considerably simpler than\n# `as.data.frame()` and hence faster\n# NOT RUN {\nif (<a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/ns-load\">requireNamespace<\/a>(\"bench\", quietly = TRUE)) {\n  l2 &lt;- <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/lapply\">replicate<\/a>(26, <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/sample\">sample<\/a>(letters), simplify = FALSE)\n  <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/names\">names<\/a>(l2) &lt;- letters\n  bench::<a href=\"https:\/\/www.rdocumentation.org\/packages\/bench\/topics\/mark\">mark<\/a>(\n    as_tibble(l2, .name_repair = \"universal\"),\n    as_tibble(l2, .name_repair = \"unique\"),\n    as_tibble(l2, .name_repair = \"minimal\"),\n    as_tibble(l2),\n    <a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/topics\/as.data.frame\">as.data.frame<\/a>(l2),\n    check = FALSE\n  )\n}\n# }<\/pre>\n\n\n\n<p><a href=\"https:\/\/tibble.tidyverse.org\/reference\/as_tibble.html\">https:\/\/tibble.tidyverse.org\/reference\/as_tibble.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coerce lists, matrices, and more to data frames Source: R\/as_tibble.R as_tibble() turns an existing object, such as a data frame, list, or matrix, into a&hellip; <\/p>\n","protected":false},"author":1,"featured_media":958,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-1136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/1136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=1136"}],"version-history":[{"count":1,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/1136\/revisions"}],"predecessor-version":[{"id":1244,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/1136\/revisions\/1244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media\/958"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=1136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=1136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=1136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}