tibble( )
Create a tidy data frame from column vectors. tibble() is from the tibble package, which is part of the tidyverse.
Required Library
install.packages("tidyverse")
library(tidyverse)Syntax
tibble(col1 = values, col2 = values, ...)tibble(col1 = values, col2 = values, ...) creates a data frame from named column vectors. A tibble is the tidyverse version of a data.frame — it has the same structure but with safer defaults and cleaner printing.
as_tibble(x)as_tibble(x) converts an existing base R data.frame (or matrix) into a tibble.
Examples
Create a tibble
Columns that reference earlier columns
Repeating values
Converting an existing data frame or list into a tibble
Add columns or rows
Original tibble
Add a new row
Add a new column
Argument Overview
Required arguments must be included when using a function while optional arguments can be included on demand.
… — name–value pairs name = vector Required
Each argument defines one column. The name becomes the column name; the value must be a vector (or single value, which is recycled to the length of the longest column).
Key rules: - All vectors must be the same length, or length 1 (recycled) - Later arguments can reference earlier column names by name - Column names can contain spaces if wrapped in backticks: `fill mass (mg)` = ... (not recommended — use snake_case with units instead)
tibble(
time_min = c(0, 15, 30, 45, 60), # 5 values
dose_mg = 200 # recycled to 5 rows
)
Data Types: any vector type — numeric, character, logical, integer
x — object to convert (as_tibble only) data.frame | matrix | list Required
The object to convert into a tibble. Typically used to convert a data.frame returned by base R functions like read.csv() into the tidyverse format.
df <- read.csv("data.csv") # returns a data.frame
tbl <- as_tibble(df) # convert for use with dplyr / tidyr
Data Types: data.frame | matrix | list
.rows — number of rows integer Optional
The number of rows the tibble should have. Useful for creating an empty tibble with a known size:
tibble(.rows = 10) # empty tibble with 10 rows and no columns
Data Types: integer