rguides

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. The sequential evaluation of columns means you can reference earlier columns when defining later ones, which is a key difference from base R’s data.frame().

.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.

The .rows parameter is a safety check that validates all columns conform to the expected row count. When the check passes, the tibble is created normally; when it fails, the function produces a clear error message that identifies which column caused the mismatch.

.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 using make.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

The "universal" repair strategy converts every column name into a syntactically valid R identifier by replacing spaces and special characters with dots or underscores. This guarantees that downstream operations using $ or non-standard evaluation will work without unexpected name-related errors.

tibble vs data.frame

Tibbles differ from base data.frame in several important ways.

Featuretibbledata.frame
PrintingShows first 10 rows and columns that fitPrints all rows and columns
Partial matchingErrorsReturns NULL silently
Unknown column accessErrorsReturns NULL silently
Factor levelsPreserved as-isDrops unused levels silently
Row namesNot supportedSupported
Vector recyclingErrors unless length 1Silent 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. By throwing errors for partial column name matching and incorrect vector lengths, tibbles help you detect data problems at the point of creation instead of discovering them much later during analysis.

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

The rownames_to_column() function converts the implicit row identifiers into an explicit column, which is the tidyverse-compatible way to preserve row identity information when migrating from base R data frames.

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

The standard tibble() call names each column explicitly, which makes the data structure self-documenting. When you have a small set of values to enter by hand, the row-oriented tribble() syntax often reads more naturally.

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

The tribble() syntax uses tildes to mark column headers and places each row on its own line, which makes the data layout visible directly in the source code. This format is especially handy for embedding small reference tables or test fixtures within your R scripts.

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 condition
  • pivot_longer(), reshape data from wide to long format
  • rownames() — get or set row names of a data frame or tibble