Open In App

Convert Tibble to Data Frame in R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Tibbles are a type of data frame in R Programming Language that has an enhanced print method, making it easier to display data. However, in some situations, we may need to convert a tibble to a data frame. So, in this article, we will explore some approaches to convert tibbles to data frames.

  • Tibble: tibble is a modern version of a data frame in R, provided by the tibble package. Tibbles have better printing and subsetting behavior than traditional Data frames.
  • Data Frame: A data frame is a data structure in R that is used for storing data in a tabular way. It is a collection of vectors of equal length, representing columns, and is similar to a table in a database.

In R there are two ways by which we can convert Tibble to Data Frame.

  1. Using as. data.frame() function
  2. Using data.frame() function

Using as. data.frame() function

In this approach, we will be using as. data.frame() function. This function is used to convert objects to data frames. This function can be used to convert various types of objects, including matrices, lists, and tibbles, to data frames. When we apply as.data.frame() to a tibble, it converts the tibble to a data frame by preserving the data structure and attributes of tibble.

In below code snippet we have created sample tibble and we are passing it to as.data.frame() function which will convert the tibble into a data frame.

R




# Load the tibble and dplyr packages
library(tibble)
library(dplyr)
 
# Create a Tibble
tibble_data <- tibble( z = c("john", "smith", "virat", "rohit", "ayyer"),
                       c(10, 20, 30, 40, 50))
 
tibble_data
print(class(tibble_data))
df_data <- as.data.frame(tibble_data)
 
# Print the Data Frame
print(df_data)
print(class(df_data))


Output:

A tibble: 5 × 2
  z     `c(10, 20, 30, 40, 50)`
  <chr>                   <dbl>
1 john                       10
2 smith                      20
3 virat                      30
4 rohit                      40
5 ayyer                      50

[1] "tbl_df"     "tbl"        "data.frame"

      z c.10..20..30..40..50.
1  john                    10
2 smith                    20
3 virat                    30
4 rohit                    40
5 ayyer                    50

[1] "data.frame"

Using data.frame() function

In this approach we will utilize The data.frame() function to convert tibble to a data frame.Usually this function is used to create data frames in R. This function can combine vectors, matrices, or other data frames into a single data frame.

below example we have created a sample tibble which we have passed to The data.frame() function as argument. This will result into conversion of tibble into a data frame.

R




# Create a Tibble
library(tibble)
tibble_data <- tibble(
  Name = c("john", "smith", "virat", "rohit", "ayyer"),
  Age = c(32, 25, 30, 29, 20),
  ID = c("a", "b", "c", "d", "e")
)
tibble_data
print(class(tibble_data))
# Convert Tibble to Data Frame
df_data <- data.frame(tibble_data)
 
# Print the Data Frame
print(df_data)
print(class(df_data))


Output:

A tibble: 5 × 3
  Name    Age ID   
  <chr> <dbl> <chr>
1 john     32 a    
2 smith    25 b    
3 virat    30 c    
4 rohit    29 d    
5 ayyer    20 e    

[1] "tbl_df"     "tbl"        "data.frame"

   Name Age ID
1  john  32  a
2 smith  25  b
3 virat  30  c
4 rohit  29  d
5 ayyer  20  e

[1] "data.frame"

Below code converts the combined Tibbles and Vectors to data frame.

R




library(tibble)
 
# Create a sample Tibble
sample_tibble <- tibble(
  A = c(1, 2, 3),
  B = c("apple", "banana", "cherry")
)
 
# Create a vector
vector_c <- c("cat", "dog", "elephant")
 
# Convert Tibble and vector to a Data Frame using data.frame() function
df <- data.frame(sample_tibble, C = vector_c)
 
# Print the combined Data Frame
print(df)


Output:

  A      B        C
1 1  apple      cat
2 2 banana      dog
3 3 cherry elephant

In below example we will convert a combined Tibble and matrix to a data frame.

R




library(tibble)
 
# Create a sample Tibble
sample_tibble <- tibble(
  A = c(1, 2, 3),
  B = c("apple", "banana", "cherry")
)
 
# Create a sample matrix
sample_matrix <- matrix(4:9, ncol = 2)
 
# Convert Tibble and matrix to a Data Frame using data.frame() function
df <- data.frame(sample_tibble, sample_matrix)
 
# Print the combined Data Frame
print(df)


Output:

  A      B X1 X2
1 1  apple  4  7
2 2 banana  5  8
3 3 cherry  6  9

Conclusion

Converting a tibble to a data frame in R using the as.data.frame function. While tibbles offer enhanced functionality and a more user-friendly display, there might be situations where using a traditional data frame is necessary or preferred. The conversion allows for compatibility with functions or packages that specifically expect a standard data frame structure. The seamless interchangeability between tibbles and data frames in R provides flexibility for data manipulation and analysis within the broader R ecosystem.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads