Open In App

Convert an Array to a DataFrame using R

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output:

A tibble: 3 × 4
  first_name last_name   age city      
  <chr>      <chr>     <dbl> <chr>     
1 Saurabh    Puri         24 pathardi  
2 Prasad     Bade         22 Beed      
3 Manohar    Khedkar      27 Ahmednagar

Create a tibble 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.

Creating tibble from Data frame

R




#create tibble from existing data frame
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 the tibble
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         
  <chr>   <dbl> <chr>        
1 Saurabh    25 New York     
2 Prasad     30 San Francisco
3 Manohar    35 Los Angeles  

Creating tibble from matrix

R




#import tibble package
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 the tibble
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
  <int> <int> <int>
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

Conclusion

In conlusion, tibbles are a modern and enhanced version of data frames in R, offers consistent way to print and understand the data. tibbles are preferred when we have to work with large dataset.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads