How to use as. data.frame() function In R Language

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"

Convert Tibble to Data Frame in R

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

Similar Reads

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....

Using data.frame() function

...

Contact Us