as_tibble()
turns an existing object, such as a data frame, list, or
matrix, into a so-called tibble, a data frame with class tbl_df
. This is
in contrast with tibble()
, which builds a tibble from individual columns.
as_tibble()
is to tibble()
as base::as.data.frame()
is to
base::data.frame()
.
as_tibble()
is an S3 generic, with methods for:
data.frame
: Thin wrapper around the list
method
that implements tibble's treatment of rownames.
list
Default: An atomic vector is first coerced to a list and, unlike
base::as.data.frame()
, the returned tibble has one column per element.
Other inputs are first coerced with base::as.data.frame()
.
as_tibble(x, ..., .rows = NULL, .name_repair = c("check_unique", "unique", "universal", "minimal"), rownames = pkgconfig::get_config("tibble::rownames", NULL)) # S3 method for data.frame as_tibble(x, validate = NULL, ..., .rows = NULL, .name_repair = c("check_unique", "unique", "universal", "minimal"), rownames = pkgconfig::get_config("tibble::rownames", NULL)) # S3 method for list as_tibble(x, validate = NULL, ..., .rows = NULL, .name_repair = c("check_unique", "unique", "universal", "minimal")) # S3 method for matrix as_tibble(x, ..., validate = NULL, .name_repair = NULL) # S3 method for table as_tibble(x, `_n` = "n", ..., n = `_n`) # S3 method for NULL as_tibble(x, ...) # S3 method for default as_tibble(x, ...)
x | A data frame, list, matrix, or other object that could reasonably be coerced to a tibble. |
---|---|
... | Other arguments passed on to individual methods. |
.rows | The number of rows, useful to create a 0-column tibble or just as an additional check. |
.name_repair | Treatment of problematic column names:
See name-repair for more details on these terms and the strategies used to enforce them. |
rownames | How to treat existing row names of a data frame or matrix:
|
_n, validate | For compatibility only, do not use for new code. |
n | Name for count column, default: |
The default behavior is to silently remove row names.
New code should explicitly convert row names to a new column using the
rownames
argument.
For existing code that relies on the retention of row names, call
pkgconfig::set_config("tibble::rownames" = NA)
in your script or in your
package's .onLoad()
function.
tibble()
constructs a tibble from individual columns. enframe()
converts a named vector to a tibble with a column of names and column of
values. name-repair documents the details of name repair.
l <- list(x = 1:500, y = runif(500), z = 500:1) df <- as_tibble(l) m <- matrix(rnorm(50), ncol = 5) colnames(m) <- c("a", "b", "c", "d", "e") df <- as_tibble(m) as_tibble(as.list(1:3), .name_repair = "unique")#>#> #> #>#> # A tibble: 1 x 3 #> ...1 ...2 ...3 #> <int> <int> <int> #> 1 1 2 3#> # A tibble: 3 x 2 #> name value #> <int> <int> #> 1 1 1 #> 2 2 2 #> 3 3 3#> # A tibble: 3 x 1 #> value #> <int> #> 1 1 #> 2 2 #> 3 3# For list-like inputs, `as_tibble()` is considerably simpler than # `as.data.frame()` and hence faster if (FALSE) { if (requireNamespace("bench", quietly = TRUE)) { l2 <- replicate(26, sample(letters), simplify = FALSE) names(l2) <- letters bench::mark( as_tibble(l2, .name_repair = "universal"), as_tibble(l2, .name_repair = "unique"), as_tibble(l2, .name_repair = "minimal"), as_tibble(l2), as.data.frame(l2), check = FALSE ) } }