1% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/tibble.R
3\name{tibble}
4\alias{tibble}
5\alias{tibble_row}
6\title{Build a data frame}
7\usage{
8tibble(
9  ...,
10  .rows = NULL,
11  .name_repair = c("check_unique", "unique", "universal", "minimal")
12)
13
14tibble_row(
15  ...,
16  .name_repair = c("check_unique", "unique", "universal", "minimal")
17)
18}
19\arguments{
20\item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}>
21A set of name-value pairs. These arguments are
22processed with \code{\link[rlang:nse-defuse]{rlang::quos()}} and support unquote via \code{\link{!!}} and
23unquote-splice via \code{\link{!!!}}. Use \verb{:=} to create columns that start with a dot.
24
25Arguments are evaluated sequentially.
26You can refer to previously created elements directly or using the \link{.data}
27pronoun.
28To refer explicitly to objects in the calling environment, use \code{\link{!!}} or
29\link{.env}, e.g. \code{!!.data} or \code{.env$.data} for the special case of an object
30named \code{.data}.}
31
32\item{.rows}{The number of rows, useful to create a 0-column tibble or
33just as an additional check.}
34
35\item{.name_repair}{Treatment of problematic column names:
36\itemize{
37\item \code{"minimal"}: No name repair or checks, beyond basic existence,
38\item \code{"unique"}: Make sure names are unique and not empty,
39\item \code{"check_unique"}: (default value), no name repair, but check they are
40\code{unique},
41\item \code{"universal"}: Make the names \code{unique} and syntactic
42\item a function: apply custom name repair (e.g., \code{.name_repair = make.names}
43for names in the style of base R).
44\item A purrr-style anonymous function, see \code{\link[rlang:as_function]{rlang::as_function()}}
45}
46
47This argument is passed on as \code{repair} to \code{\link[vctrs:vec_as_names]{vctrs::vec_as_names()}}.
48See there for more details on these terms and the strategies used
49to enforce them.}
50}
51\value{
52A tibble, which is a colloquial term for an object of class
53\code{\link[=tbl_df-class]{tbl_df}}. A \code{\link[=tbl_df-class]{tbl_df}} object is also a data
54frame, i.e. it has class \code{data.frame}.
55}
56\description{
57\code{tibble()} constructs a data frame. It is used like \code{\link[base:data.frame]{base::data.frame()}}, but
58with a couple notable differences:
59\itemize{
60\item The returned data frame has the class \code{\link[=tbl_df-class]{tbl_df}}, in
61addition to \code{data.frame}. This allows so-called "tibbles" to exhibit some
62special behaviour, such as \link[=formatting]{enhanced printing}. Tibbles are
63fully described in \code{\link[=tbl_df-class]{tbl_df}}.
64\item \code{tibble()} is much lazier than \code{\link[base:data.frame]{base::data.frame()}} in terms of
65transforming the user's input.
66\itemize{
67\item Character vectors are not coerced to factor.
68\item List-columns are expressly anticipated and do not require special tricks.
69\item Column names are not modified.
70\item Inner names in columns are left unchanged.
71}
72\item \code{tibble()} builds columns sequentially. When defining a column, you can
73refer to columns created earlier in the call. Only columns of length one
74are recycled.
75\item If a column evaluates to a data frame or tibble, it is nested or spliced.
76See examples.
77}
78
79\code{tibble_row()} constructs a data frame that is guaranteed to occupy one row.
80Vector columns are required to have size one, non-vector columns are wrapped
81in a list.
82}
83\examples{
84# Unnamed arguments are named with their expression:
85a <- 1:5
86tibble(a, a * 2)
87
88# Scalars (vectors of length one) are recycled:
89tibble(a, b = a * 2, c = 1)
90
91# Columns are available in subsequent expressions:
92tibble(x = runif(10), y = x * 2)
93
94# tibble() never coerces its inputs,
95str(tibble(letters))
96str(tibble(x = list(diag(1), diag(2))))
97
98# or munges column names (unless requested),
99tibble(`a + b` = 1:5)
100
101# but it forces you to take charge of names, if they need repair:
102try(tibble(x = 1, x = 2))
103tibble(x = 1, x = 2, .name_repair = "unique")
104tibble(x = 1, x = 2, .name_repair = "minimal")
105
106## By default, non-syntactic names are allowed,
107df <- tibble(`a 1` = 1, `a 2` = 2)
108## because you can still index by name:
109df[["a 1"]]
110df$`a 1`
111with(df, `a 1`)
112
113## Syntactic names are easier to work with, though, and you can request them:
114df <- tibble(`a 1` = 1, `a 2` = 2, .name_repair = "universal")
115df$a.1
116
117## You can specify your own name repair function:
118tibble(x = 1, x = 2, .name_repair = make.unique)
119
120fix_names <- function(x) gsub("\\\\s+", "_", x)
121tibble(`year 1` = 1, `year 2` = 2, .name_repair = fix_names)
122
123## purrr-style anonymous functions and constants
124## are also supported
125tibble(x = 1, x = 2, .name_repair = ~ make.names(., unique = TRUE))
126
127tibble(x = 1, x = 2, .name_repair = ~ c("a", "b"))
128
129# Tibbles can contain columns that are tibbles or matrices
130# if the number of rows is compatible. Unnamed tibbled are
131# spliced, i.e. the inner columns are inserted into the
132# tibble under construction.
133tibble(
134  a = 1:3,
135  tibble(
136    b = 4:6,
137    c = 7:9
138  ),
139  d = tibble(
140    e = tibble(
141      f = b
142    )
143  )
144)
145tibble(
146  a = 1:3,
147  b = diag(3),
148  c = cor(trees)
149)
150
151# data can not contain POSIXlt columns, or tibbles or matrices
152# with incompatible number of rows:
153try(tibble(y = strptime("2000/01/01", "\%x")))
154try(tibble(a = 1:3, b = tibble(c = 4:7)))
155
156# Use := to create columns with names that start with a dot:
157tibble(.dotted = 3)
158tibble(.dotted := 3)
159
160# You can unquote an expression:
161x <- 3
162tibble(x = 1, y = x)
163tibble(x = 1, y = !!x)
164
165# You can splice-unquote a list of quosures and expressions:
166tibble(!!! list(x = rlang::quo(1:10), y = quote(x * 2)))
167
168# Use .data, .env and !! to refer explicitly to columns or outside objects
169a <- 1
170tibble(a = 2, b = a)
171tibble(a = 2, b = .data$a)
172tibble(a = 2, b = .env$a)
173tibble(a = 2, b = !!a)
174try(tibble(a = 2, b = .env$bogus))
175try(tibble(a = 2, b = !!bogus))
176
177# Use tibble_row() to construct a one-row tibble:
178tibble_row(a = 1, lm = lm(Height ~ Girth + Volume, data = trees))
179}
180\seealso{
181Use \code{\link[=as_tibble]{as_tibble()}} to turn an existing object into a tibble. Use
182\code{enframe()} to convert a named vector into a tibble. Name repair is
183detailed in \code{\link[vctrs:vec_as_names]{vctrs::vec_as_names()}}.
184See \link{quasiquotation} for more details on tidy dots semantics,
185i.e. exactly how  the \code{...} argument is processed.
186}
187