as_tibble()
turns an existing object, such as a data frame 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.
Default: Other inputs are first coerced with base::as.data.frame()
.
as_tibble_row()
converts a vector to a tibble with one row.
If the input is a list, all elements must have size one.
as_tibble_col()
converts a vector to a tibble with one column.
# S3 method for class 'SummarizedExperiment'
as_tibble(
x,
...,
.name_repair = c("check_unique", "unique", "universal", "minimal"),
rownames = pkgconfig::get_config("tibble::rownames", NULL)
)
A data frame, list, matrix, or other object that could reasonably be coerced to a tibble.
Unused, for extensibility.
Treatment of problematic column names:
"minimal"
: No name repair or checks, beyond basic existence,
"unique"
: Make sure names are unique and not empty,
"check_unique"
: (default value), no name repair, but check they are
unique
,
"universal"
: Make the names unique
and syntactic
"unique_quiet"
: Same as "unique"
, but "quiet"
"universal_quiet"
: Same as "universal"
, but "quiet"
a function: apply custom name repair (e.g., .name_repair = make.names
for names in the style of base R).
A purrr-style anonymous function, see rlang::as_function()
This argument is passed on as repair
to vctrs::vec_as_names()
.
See there for more details on these terms and the strategies used
to enforce them.
How to treat existing row names of a data frame or matrix:
NULL
: remove row names. This is the default.
NA
: keep row names.
A string: the name of a new column. Existing rownames are transferred
into this column and the row.names
attribute is deleted.
No name repair is applied to the new column name, even if x
already contains
a column of that name.
Use as_tibble(rownames_to_column(...))
to safeguard against this case.
Read more in rownames.
tibble
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.
Using as_tibble()
for vectors is superseded as of version 3.0.0,
prefer the more expressive as_tibble_row()
and
as_tibble_col()
variants for new code.
Hutchison, W.J., Keyes, T.J., The tidyomics Consortium. et al. The tidyomics ecosystem: enhancing omic data analyses. Nat Methods 21, 1166–1170 (2024). https://doi.org/10.1038/s41592-024-02299-2
Müller, K., Wickham, H. (2023). tibble: Simple Data Frames. R package version 3.2.1, https://CRAN.R-project.org/package=tibble
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 is implemented using vctrs::vec_as_names()
.
tidySummarizedExperiment::pasilla %>%
as_tibble()
#> # A tibble: 102,193 × 5
#> .feature .sample counts condition type
#> <chr> <chr> <int> <chr> <chr>
#> 1 FBgn0000003 untrt1 0 untreated single_end
#> 2 FBgn0000008 untrt1 92 untreated single_end
#> 3 FBgn0000014 untrt1 5 untreated single_end
#> 4 FBgn0000015 untrt1 0 untreated single_end
#> 5 FBgn0000017 untrt1 4664 untreated single_end
#> 6 FBgn0000018 untrt1 583 untreated single_end
#> 7 FBgn0000022 untrt1 0 untreated single_end
#> 8 FBgn0000024 untrt1 10 untreated single_end
#> 9 FBgn0000028 untrt1 0 untreated single_end
#> 10 FBgn0000032 untrt1 1446 untreated single_end
#> # ℹ 102,183 more rows
tidySummarizedExperiment::pasilla %>%
as_tibble(.subset=-c(condition, type))
#> # A tibble: 102,193 × 3
#> .feature .sample counts
#> <chr> <chr> <int>
#> 1 FBgn0000003 untrt1 0
#> 2 FBgn0000008 untrt1 92
#> 3 FBgn0000014 untrt1 5
#> 4 FBgn0000015 untrt1 0
#> 5 FBgn0000017 untrt1 4664
#> 6 FBgn0000018 untrt1 583
#> 7 FBgn0000022 untrt1 0
#> 8 FBgn0000024 untrt1 10
#> 9 FBgn0000028 untrt1 0
#> 10 FBgn0000032 untrt1 1446
#> # ℹ 102,183 more rows