/
tibble.Rd
120 lines (103 loc) · 3.6 KB
/
tibble.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/tibble.R
\name{tibble}
\alias{tibble}
\alias{tbl_df-class}
\title{Build a data frame}
\usage{
tibble(..., .rows = NULL, .name_repair = c("check_unique", "unique",
"syntactic", "minimal"))
}
\arguments{
\item{...}{A set of name-value pairs. Arguments are evaluated sequentially,
so you can refer to previously created elements. These arguments are
processed with \code{\link[rlang:quos]{rlang::quos()}} and support unquote via \code{!!} and
unquote-splice via \code{\link{!!!}}.}
\item{.rows}{The number of rows, useful to create a 0-column tibble or
just as an additional check.}
\item{.name_repair}{Treatment of problematic column names:
\itemize{
\item \code{"minimal"}: No name repair or checks, beyond basic existence,
\item \code{"unique"}: Make sure names are unique and not empty,
\item \code{"check_unique"}: (default value), no name repair, but check they are \code{unique},
\item \code{"syntactic"}: Make the names \code{unique} and syntactic
\item a function: apply custom name repair (e.g., \code{.name_repair = make.names}
for names in the style of base R).
\item A purrr-style anonymous function, see \code{\link[=as_function]{as_function()}}
}
See \link{name-repair} for more details on these terms and the strategies used
to enforce them.}
}
\description{
\code{tibble()} is a trimmed down version of \code{\link[=data.frame]{data.frame()}} that:
\itemize{
\item Never coerces inputs (i.e. strings stay as strings!).
\item Never adds \code{row.names}.
\item Only recycles length 1 inputs.
\item Automatically adds column names.
\item Doesn't munge column names.
\item Evaluates its arguments lazily and in order.
\item Adds \code{tbl_df} class to output.
}
}
\examples{
# Unnamed arguments are named with their expression:
a <- 1:5
tibble(a, a * 2)
# Scalars (vectors of length one) are recycled:
tibble(a, b = a * 2, c = 1)
# Columns are available in subsequent expressions:
tibble(x = runif(10), y = x * 2)
# tibble() never coerces its inputs,
str(tibble(letters))
str(tibble(x = list(diag(1), diag(2))))
# or munges column names (unless requested),
tibble(`a + b` = 1:5)
# but it forces you to take charge of names, if they need repair:
try(tibble(x = 1, x = 2))
tibble(x = 1, x = 2, .name_repair = "unique")
tibble(x = 1, x = 2, .name_repair = "minimal")
## By default, non-syntactic names are allowed,
df <- tibble(`a 1` = 1, `a 2` = 2)
## because you can still index by name:
df[["a 1"]]
df$`a 1`
with(df, `a 1`)
## Syntactic names are easier to work with, though, and you can request them:
df <- tibble(`a 1` = 1, `a 2` = 2, .name_repair = "syntactic")
df$a.1
## You can specify your own name repair function:
tibble(x = 1, x = 2, .name_repair = make.unique)
tibble(x = 1, x = 2, .name_repair = ~make.names(., unique = TRUE))
fix_names <- function(x) gsub("\\\\s+", "_", x)
tibble(`year 1` = 1, `year 2` = 2, .name_repair = fix_names)
# Tibbles can contain columns that are tibbles or matrices
# if the number of rows is consistent:
tibble(
a = 1:3,
b = tibble(
c = 4:6,
d = 7:9
),
e = tibble(
f = tibble(
g = letters[1:3]
)
)
)
tibble(
a = 1:4,
b = diag(4),
c = cov(iris[1:4])
)
# data can not contain POSIXlt columns, or tibbles or matrices
# with inconsistent number of rows:
try(tibble(y = strptime("2000/01/01", "\%x")))
try(tibble(a = 1:3, b = tibble(c = 4:7)))
# You can splice-unquote a list of quosures and expressions:
tibble(!!!list(x = rlang::quo(1:10), y = quote(x * 2)))
}
\seealso{
\code{\link[=as_tibble]{as_tibble()}} to turn an existing list into a data frame,
\link{name-repair} for more detail on name repair.
}