tibble
tibble(..., .rows = NULL, .name_repair = c('check_unique', 'unique', 'universal', 'minimal')) Description
tibble() constructs a tibble from individual vectors or expressions. A tibble is a modern reimagining of the base R data.frame, designed to avoid some of its historical quirks while providing a more predictable and consistent interface.
Tibbles are the standard data structure in the tidyverse. Functions like dplyr::filter(), dplyr::mutate(), and tidyr::pivot_longer() all expect tibble input (or coercible to tibble).
Arguments
…
Named arguments become columns of the tibble. Each argument is evaluated once, in order, so earlier columns are available for use in later expressions.
tibble(
name = c("Alice", "Bob", "Carol"),
age = c(29, 34, 27),
active = c(TRUE, FALSE, TRUE)
)
# # A tibble: 3 × 3
# name age active
# <chr> <dbl> <lgl>
# 1 Alice 29 TRUE
# 2 Bob 34 FALSE
# 3 Carol 27 TRUE
Unnamed arguments are auto-named with their expression text, which is rarely desirable, so prefer named arguments.
.rows
Number of rows. Defaults to NULL. When set, the tibble checks that all columns have length 1 or the specified number of rows. Use this as an assertion to validate dimensions. Added in tibble 3.0.0.
# Valid: .rows matches column length
tibble(x = 1:3, y = letters[1:3], .rows = 3)
# # A tibble: 3 × 2
# x y
# <int> <chr>
# 1 1 a
# 2 2 b
# 3 3 c
# Error: column length does not match .rows
tibble(x = 1:3, .rows = 5)
# Error: tibble column `x` must have size 5, not 3.
.name_repair
How to handle column names that are missing, duplicated, or non-syntactic. Options:
"check_unique"(default) — errors if any name is duplicated."unique"— appends...1,...2, etc. to duplicated names instead of erroring."universal"— makes all names syntactically valid usingmake.names()."minimal"— leaves names as-is; duplicates will cause problems downstream.- A custom function — receives a character vector of names and must return a repaired character vector of the same length.
# Non-syntactic names are repaired with "universal"
tibble(
"first name" = "Alice",
"123" = 1,
"" = "empty",
.name_repair = "universal"
)
# # A tibble: 1 × 3
# first.name X123 X
# <chr> <dbl> <chr>
# 1 Alice 1 empty
tibble vs data.frame
Tibbles differ from base data.frame in several important ways.
| Feature | tibble | data.frame |
|---|---|---|
| Printing | Shows first 10 rows and columns that fit | Prints all rows and columns |
| Partial matching | Errors | Returns NULL silently |
| Unknown column access | Errors | Returns NULL silently |
| Factor levels | Preserved as-is | Drops unused levels silently |
| Row names | Not supported | Supported |
| Vector recycling | Errors unless length 1 | Silent with a warning |
# tibble is strict about partial column names
df <- tibble(count = 1:3)
df$cou
# Error: Must use a single string to index a column, not a character vector.
# tibble refuses to recycle vectors of wrong length
tibble(x = 1:3, y = 1:2)
# Error: Tibble columns must have the same size.
# * `x` has size 3.
# * `y` has size 2.
These behaviors catch bugs early rather than producing silent mismatches.
Row Names
Tibbles do not support row names. If you need them, convert to a column explicitly with tibble::rownames_to_column():
df <- tibble(x = 1:3, y = c("a", "b", "c"))
rownames_to_column(df, var = "id")
# # A tibble: 3 × 3
# id x y
# <chr> <int> <chr>
# 1 1 1 a
# 2 2 2 b
# 3 3 3 c
Value
tibble() returns a tibble with one column per argument. Column order is preserved. The result has class c("tbl_df", "tbl", "data.frame"), so it is compatible with both tidyverse and base R functions that accept data frames.
Examples
Basic creation
tibble(
name = c("Alice", "Bob", "Carol"),
age = c(29, 34, 41),
active = c(TRUE, FALSE, TRUE)
)
# # A tibble: 3 × 3
# name age active
# <chr> <dbl> <lgl>
# 1 Alice 29 TRUE
# 2 Bob 34 FALSE
# 3 Carol 41 TRUE
Row-oriented creation with tribble
tribble() defines tibbles in a row-oriented layout — convenient for small lookup tables:
tribble(
~name, ~score, ~passed,
"Alice", 88, TRUE,
"Bob", 72, FALSE,
"Carol", 95, TRUE
)
# # A tibble: 3 × 3
# name score passed
# <chr> <dbl> <lgl>
# 1 Alice 88 TRUE
# 2 Bob 72 FALSE
# 3 Carol 95 TRUE
Converting existing objects
as_tibble() converts matrices, lists, or data frames to tibbles:
# From a data frame
as_tibble(mtcars)
# From a matrix (each column becomes a column)
as_tibble(matrix(1:9, nrow = 3, byrow = TRUE))
See Also
filter()— filter rows of a tibble by conditionpivot_longer()— reshape data from wide to long formatrownames()— get or set row names of a data frame or tibble