Convert an Array to a DataFrame using R
Last Updated :
24 Apr, 2025
In this article, we will see what is Tibbles in R Programming Language and different ways to create tibbles.
Tibble is a modern data frame that is similar to data frames in R Programming Language but with some enhancements to make them easier to use and more consistent. Tibble is a part of the tidyverse package in R. Using tibbles we can view and understand the data very easily especially when working with large datasets.
We have to install the tibble package to use it. the following command is used for installing.
install.packages("tibble")
Different ways to create Tibbles in R
- Using tibble() function
- Using tribble() function
- Using as_tibble() function
Create Tibble using tibble() function
In this example we will create a new tibble from individual vectors using tibble() function. Here we are creating vectors such as Id, Name, Age, Role, Salary and passing them to tibble function, so it will convert that individual vectors into columns.
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 the tibble
print(my_tib)
Output:
A tibble: 4 × 5
Id Name Age Role Salary
<int> <chr> <dbl> <chr> <dbl>
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
Create Tibble using tribble function
In this method we will use tribble() function to create a tibble. tribble() function is used when we have small amount of data. In tibble we can give names to columns by preceding them with tilde symbol.
In below example, we have created a tibble with four columns: first_name, last_name, age, and city.
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 the tibble
print(my_tib)