tibble

repository·main·Indexed 20 days ago

https://github.com/tidyverse/tibble

A modern reimagining of the R data.frame designed to be more predictable by avoiding automatic type conversion, partial matching, and the creation of row names. It provides functions such as as_tibble() for conversion, tibble() for creating tibbles from column vectors, and tribble() for row-by-row data entry.

Tokens
684
Snippets
4
Records
5
Agent score
23%

What's inside tibble

  1. What is a tibble?

    main

    A tibble (or tbl_df) is a modern reimagining of the R data.frame. It is designed to be "lazy and surly," meaning it performs fewer automatic transformations than a standard data.frame to help prevent silent errors:

    • Does less: It does not change variable names or types, does not perform partial matching, and does not create row.names().
    • Complains more: It will throw errors or warnings when a variable does not exist rather than attempting to guess.
    • Enhanced printing: The print() method is optimized for large datasets and complex objects, showing column types and a concise summary.

    If you are new to tibbles, the tibbles chapter in R for Data Science is the recommended starting point.

  2. Install tibble

    main

    You can install tibble via CRAN or the development version from GitHub.

    To install the entire tidyverse suite:

    install.packages("tidyverse")

    To install only the tibble package:

    install.packages("tibble")

    To install the development version from GitHub using pak:

    # install.packages("pak")
    pak::pak("tidyverse/tibble")
    install.packages("tibble")
    # Or for development version:
    pak::pak("tidyverse/tibble")
  3. Create a new tibble with tibble()

    main

    Use tibble() to create a new tibble from column vectors. Unlike data.frame(), tibble() follows stricter rules:

    • It never changes the type of inputs (e.g., it preserves list columns).
    • It never changes variable names.
    • It only recycles inputs of length 1.
    • It never creates row.names().
    tibble(x = 1:5, y = 1, z = x^2 + y)
    #> # A tibble: 5 × 3
    #>       x     y     z
    #>   <int> <dbl> <dbl>
    #> 1     1     1     2
    #> 2     2     1     5
    #> 3     3     1    10
    #> 4     4     1    17
    #> 5     5     1    26
    tibble(x = 1:5, y = 1, z = x^2 + y)
  4. Convert an object to a tibble with as_tibble()

    main

    Use as_tibble() to convert existing objects (such as data.frame, list, matrix, or table) into a tibble format.

    data <- data.frame(a = 1:3, b = letters[1:3], c = Sys.Date() - 1:3)
    as_tibble(data)
    #> # A tibble: 3 × 3
    #>       a b     c         
    #>   <int> <chr> <date>    
    #> 1     1 a     2025-06-18
    #> 2     2 b     2025-06-17
    #> 3     3 c     2025-06-16
    as_tibble(data)
  5. Create a tibble row-by-row with tribble()

    main

    Use tribble() to define a tibble row-by-row, which is useful for manual data entry where the visual structure of the code matches the table structure. Use the tilde (~) prefix for column names.

    tribble(
      ~x, ~y,  ~z,
      "a", 2,  3.6,
      "b", 1,  8.5
    )
    #> # A tibble: 2 × 3
    #>   x         y     z  
    #>   <chr> <dbl> <dbl>
    #> 1 a         2   3.6
    #> 2 b         1   8.5
    tribble(
      ~x, ~y,  ~z,
      "a", 2,  3.6,
      "b", 1,  8.5
    )