Open In App

Tibbles Dataframe

Last Updated : 28 Apr, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

A tibble is a more advanced data frame in R, providing a cleaner, more consistent way of handling data. It is contained within the tidyverse package and provides a better way to handle large datasets, with a cleaner, easier-to-read output. Tibbles will only display a subset of columns and rows when printed, so it is easier to manage large data.

To install tibbles, you must install the tibble package:

install.packages("tibble")

Different ways to create Tibbles in R

  • Using tibble() function
  • Using tribble() function
  • Using as_tibble() function

Using tibble() function

In this example we will create a new tibble from individual vectors using tibble() function.

R
library(tibble)

# Create a sample tibble from individual vectors
my_tib <- tibble(
  Id=1:4,
  Name = c("Sandip", "Gaurav", "Ram", "Pratik"),
  Age = c(25, 29,30, 35),
  Role = c("Engineer", "Data Scientist", "Developer", "HR"),
  Salary = c(45000,60000, 80000, 100000)
)

print(my_tib)

Output:

A tibble: 4 × 5
Id Name Age Role Salary

1 1 Sandip 25 Engineer 45000
2 2 Gaurav 29 Data Scientist 60000
3 3 Ram 30 Developer 80000
4 4 Pratik 35 HR 100000

Here we have created vectors such as Id, Name, Age, Role, Salary and passing them to tibble function, so it convert that individual vectors into columns.

Using tribble function

The function tribble() is optimal for small data sets. You can specify column names with the tilde (~) operator.

R
library(tibble)

# Create a tibble using tribble
my_tib <- tribble(
  ~first_name,   ~last_name,  ~age,  ~city,
  "Saurabh",       "Puri",     24,   "pathardi",
  "Prasad",         "Bade",   22,   "Beed",
  "Manohar",     "Khedkar",  27,   "Ahmednagar"
)

print(my_tib)

Output:

A tibble: 3 × 4
first_name last_name age city

1 Saurabh Puri 24 pathardi
2 Prasad Bade 22 Beed
3 Manohar Khedkar 27 Ahmednagar

Here we have created a tibble with four columns: first_name, last_name, age, and city.

Using as_tibble function

In this method we will use as_tibble() function to create tibble from an existing data frame or matrix. This function creates the tibble with same data as original data.

R
library(tibble)

# Create a data frame
df <- data.frame(
  name = c("Saurabh", "Prasad", "Manohar"),
  age = c(25, 30, 35),
  city = c("New York", "San Francisco", "Los Angeles")
)
print('Data Frame:')
print(df)

# Convert the data frame to a tibble
tib <- as_tibble(df)

print('Tibble:')
print(tib)

Output:

[1] "Data Frame:"
name age city
1 Saurabh 25 New York
2 Prasad 30 San Francisco
3 Manohar 35 Los Angeles
[1] "Tibble:"
A tibble: 3 × 3
name age city

1 Saurabh 25 New York
2 Prasad 30 San Francisco
3 Manohar 35 Los Angeles

Creating tibble from matrix

R
library(tibble)

# create a sample matrix
mat <- matrix(1:12, nrow = 4, ncol = 3)
mat

# convert the matrix into tibble
tib <- as_tibble(mat)
print(tib)

Output:

[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
A tibble: 4 × 3
V1 V2 V3

1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12

Difference between Tibbles and data.frame

Key pints

Tibbles

data frame

Printing

Show a few rows and columns at a time, easier to read

Show all rows and columns, can be confusing

Column names

Need to be simple and unique

Can be more flexible and allow for different styles

Subsetting

Always get a similar type of output

Output can vary based on how you ask for it

Packages

Present in the tidyverse ecosystem

Present in R base package

Related Articles


Next Article
Article Tags :

Similar Reads