1% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/as_tibble.R
3\name{as_tibble}
4\alias{as_tibble}
5\alias{as_tibble.data.frame}
6\alias{as_tibble.list}
7\alias{as_tibble.matrix}
8\alias{as_tibble.table}
9\alias{as_tibble.NULL}
10\alias{as_tibble.default}
11\alias{as_tibble_row}
12\alias{as_tibble_col}
13\title{Coerce lists, matrices, and more to data frames}
14\usage{
15as_tibble(
16  x,
17  ...,
18  .rows = NULL,
19  .name_repair = c("check_unique", "unique", "universal", "minimal"),
20  rownames = pkgconfig::get_config("tibble::rownames", NULL)
21)
22
23\method{as_tibble}{data.frame}(
24  x,
25  validate = NULL,
26  ...,
27  .rows = NULL,
28  .name_repair = c("check_unique", "unique", "universal", "minimal"),
29  rownames = pkgconfig::get_config("tibble::rownames", NULL)
30)
31
32\method{as_tibble}{list}(
33  x,
34  validate = NULL,
35  ...,
36  .rows = NULL,
37  .name_repair = c("check_unique", "unique", "universal", "minimal")
38)
39
40\method{as_tibble}{matrix}(x, ..., validate = NULL, .name_repair = NULL)
41
42\method{as_tibble}{table}(x, `_n` = "n", ..., n = `_n`, .name_repair = "check_unique")
43
44\method{as_tibble}{`NULL`}(x, ...)
45
46\method{as_tibble}{default}(x, ...)
47
48as_tibble_row(
49  x,
50  .name_repair = c("check_unique", "unique", "universal", "minimal")
51)
52
53as_tibble_col(x, column_name = "value")
54}
55\arguments{
56\item{x}{A data frame, list, matrix, or other object that could reasonably be
57coerced to a tibble.}
58
59\item{...}{Unused, for extensibility.}
60
61\item{.rows}{The number of rows, useful to create a 0-column tibble or
62just as an additional check.}
63
64\item{.name_repair}{Treatment of problematic column names:
65\itemize{
66\item \code{"minimal"}: No name repair or checks, beyond basic existence,
67\item \code{"unique"}: Make sure names are unique and not empty,
68\item \code{"check_unique"}: (default value), no name repair, but check they are
69\code{unique},
70\item \code{"universal"}: Make the names \code{unique} and syntactic
71\item a function: apply custom name repair (e.g., \code{.name_repair = make.names}
72for names in the style of base R).
73\item A purrr-style anonymous function, see \code{\link[rlang:as_function]{rlang::as_function()}}
74}
75
76This argument is passed on as \code{repair} to \code{\link[vctrs:vec_as_names]{vctrs::vec_as_names()}}.
77See there for more details on these terms and the strategies used
78to enforce them.}
79
80\item{rownames}{How to treat existing row names of a data frame or matrix:
81\itemize{
82\item \code{NULL}: remove row names. This is the default.
83\item \code{NA}: keep row names.
84\item A string: the name of a new column. Existing rownames are transferred
85into this column and the \code{row.names} attribute is deleted.
86Read more in \link{rownames}.
87}}
88
89\item{_n, validate}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#soft-deprecated}{\figure{lifecycle-soft-deprecated.svg}{options: alt='[Soft-deprecated]'}}}{\strong{[Soft-deprecated]}}
90
91For compatibility only, do not use for new code.}
92
93\item{n}{Name for count column, default: \code{"n"}.}
94
95\item{column_name}{Name of the column.}
96}
97\description{
98\code{as_tibble()} turns an existing object, such as a data frame or
99matrix, into a so-called tibble, a data frame with class \code{\link{tbl_df}}. This is
100in contrast with \code{\link[=tibble]{tibble()}}, which builds a tibble from individual columns.
101\code{as_tibble()} is to \code{\link[=tibble]{tibble()}} as \code{\link[base:as.data.frame]{base::as.data.frame()}} is to
102\code{\link[base:data.frame]{base::data.frame()}}.
103
104\code{as_tibble()} is an S3 generic, with methods for:
105\itemize{
106\item \code{\link[base:data.frame]{data.frame}}: Thin wrapper around the \code{list} method
107that implements tibble's treatment of \link{rownames}.
108\item \code{\link[methods:StructureClasses]{matrix}}, \code{\link[stats:poly]{poly}},
109\code{\link[stats:ts]{ts}}, \code{\link[base:table]{table}}
110\item Default: Other inputs are first coerced with \code{\link[base:as.data.frame]{base::as.data.frame()}}.
111}
112
113\code{as_tibble_row()} converts a vector to a tibble with one row.
114If the input is a list, all elements must have size one.
115
116\code{as_tibble_col()} converts a vector to a tibble with one column.
117}
118\section{Row names}{
119
120The default behavior is to silently remove row names.
121
122New code should explicitly convert row names to a new column using the
123\code{rownames} argument.
124
125For existing code that relies on the retention of row names, call
126\code{pkgconfig::set_config("tibble::rownames" = NA)} in your script or in your
127package's \code{\link[=.onLoad]{.onLoad()}}  function.
128}
129
130\section{Life cycle}{
131
132Using \code{as_tibble()} for vectors is superseded as of version 3.0.0,
133prefer the more expressive \code{as_tibble_row()} and
134\code{as_tibble_col()} variants for new code.
135}
136
137\examples{
138m <- matrix(rnorm(50), ncol = 5)
139colnames(m) <- c("a", "b", "c", "d", "e")
140df <- as_tibble(m)
141
142as_tibble_row(c(a = 1, b = 2))
143as_tibble_row(list(c = "three", d = list(4:5)))
144as_tibble_row(1:3, .name_repair = "unique")
145
146as_tibble_col(1:3)
147as_tibble_col(
148  list(c = "three", d = list(4:5)),
149  column_name = "data"
150)
151}
152\seealso{
153\code{\link[=tibble]{tibble()}} constructs a tibble from individual columns. \code{\link[=enframe]{enframe()}}
154converts a named vector to a tibble with a column of names and column of
155values. Name repair is implemented using \code{\link[vctrs:vec_as_names]{vctrs::vec_as_names()}}.
156}
157