-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathas_tibble.Rd
160 lines (131 loc) · 5.38 KB
/
as_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/as_tibble.R
\name{as_tibble}
\alias{as_tibble}
\alias{as_tibble.data.frame}
\alias{as_tibble.list}
\alias{as_tibble.matrix}
\alias{as_tibble.table}
\alias{as_tibble.NULL}
\alias{as_tibble.default}
\alias{as_tibble_row}
\alias{as_tibble_col}
\title{Coerce lists, matrices, and more to data frames}
\usage{
as_tibble(
x,
...,
.rows = NULL,
.name_repair = c("check_unique", "unique", "universal", "minimal"),
rownames = pkgconfig::get_config("tibble::rownames", NULL)
)
\method{as_tibble}{data.frame}(
x,
validate = NULL,
...,
.rows = NULL,
.name_repair = c("check_unique", "unique", "universal", "minimal"),
rownames = pkgconfig::get_config("tibble::rownames", NULL)
)
\method{as_tibble}{list}(
x,
validate = NULL,
...,
.rows = NULL,
.name_repair = c("check_unique", "unique", "universal", "minimal")
)
\method{as_tibble}{matrix}(x, ..., validate = NULL, .name_repair = NULL)
\method{as_tibble}{table}(x, `_n` = "n", ..., n = `_n`, .name_repair = "check_unique")
\method{as_tibble}{`NULL`}(x, ...)
\method{as_tibble}{default}(x, ...)
as_tibble_row(
x,
.name_repair = c("check_unique", "unique", "universal", "minimal")
)
as_tibble_col(x, column_name = "value")
}
\arguments{
\item{x}{A data frame, list, matrix, or other object that could reasonably be
coerced to a tibble.}
\item{...}{Unused, for extensibility.}
\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{"universal"}: 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[rlang:as_function]{rlang::as_function()}}
}
This argument is passed on as \code{repair} to \code{\link[vctrs:vec_as_names]{vctrs::vec_as_names()}}.
See there for more details on these terms and the strategies used
to enforce them.}
\item{rownames}{How to treat existing row names of a data frame or matrix:
\itemize{
\item \code{NULL}: remove row names. This is the default.
\item \code{NA}: keep row names.
\item A string: the name of a new column. Existing rownames are transferred
into this column and the \code{row.names} attribute is deleted.
No name repair is applied to the new column name, even if \code{x} already contains
a column of that name.
Use \code{as_tibble(rownames_to_column(...))} to safeguard against this case.
}
Read more in \link{rownames}.}
\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]}}
For compatibility only, do not use for new code.}
\item{n}{Name for count column, default: \code{"n"}.}
\item{column_name}{Name of the column.}
}
\description{
\code{as_tibble()} turns an existing object, such as a data frame or
matrix, into a so-called tibble, a data frame with class \code{\link{tbl_df}}. This is
in contrast with \code{\link[=tibble]{tibble()}}, which builds a tibble from individual columns.
\code{as_tibble()} is to \code{\link[=tibble]{tibble()}} as \code{\link[base:as.data.frame]{base::as.data.frame()}} is to
\code{\link[base:data.frame]{base::data.frame()}}.
\code{as_tibble()} is an S3 generic, with methods for:
\itemize{
\item \code{\link[base:data.frame]{data.frame}}: Thin wrapper around the \code{list} method
that implements tibble's treatment of \link{rownames}.
\item \code{\link[base:matrix]{matrix}}, \code{\link[stats:poly]{poly}},
\code{\link[stats:ts]{ts}}, \code{\link[base:table]{table}}
\item Default: Other inputs are first coerced with \code{\link[base:as.data.frame]{base::as.data.frame()}}.
}
\code{as_tibble_row()} converts a vector to a tibble with one row.
If the input is a list, all elements must have size one.
\code{as_tibble_col()} converts a vector to a tibble with one column.
}
\section{Row names}{
The default behavior is to silently remove row names.
New code should explicitly convert row names to a new column using the
\code{rownames} argument.
For existing code that relies on the retention of row names, call
\code{pkgconfig::set_config("tibble::rownames" = NA)} in your script or in your
package's \code{\link[=.onLoad]{.onLoad()}} function.
}
\section{Life cycle}{
Using \code{as_tibble()} for vectors is superseded as of version 3.0.0,
prefer the more expressive \code{as_tibble_row()} and
\code{as_tibble_col()} variants for new code.
}
\examples{
m <- matrix(rnorm(50), ncol = 5)
colnames(m) <- c("a", "b", "c", "d", "e")
df <- as_tibble(m)
as_tibble_row(c(a = 1, b = 2))
as_tibble_row(list(c = "three", d = list(4:5)))
as_tibble_row(1:3, .name_repair = "unique")
as_tibble_col(1:3)
as_tibble_col(
list(c = "three", d = list(4:5)),
column_name = "data"
)
}
\seealso{
\code{\link[=tibble]{tibble()}} constructs a tibble from individual columns. \code{\link[=enframe]{enframe()}}
converts a named vector to a tibble with a column of names and column of
values. Name repair is implemented using \code{\link[vctrs:vec_as_names]{vctrs::vec_as_names()}}.
}