Contents

Difference Between as_tibble, as_data_frame, and tbl_df in R

The as_tibble(), as_data_frame(), and tbl_df() functions are commonly used for data frame manipulations in R with their advantages and disadvantages.

The following example explains the differences among as_tibble(), as_data_frame(), and tbl_df() functions and which one you should use. `

as_tibble()

as_tibble() function from the tibble package in R is used for converting the data frame or matrix objects into the tibble data frame with tbl_df class.

tibble is a data frame structure that is user-friendly with enhanced output and easy to use on complex datasets. In addition, tibble is consistent within tidyverse package ecosystem such as dplyr, ggplot2, tidyr, purr, forcats, stringr, and readr.

Example of converting a standard R data frame into a tibble:

# import packages
library(tibble)

# Create a data frame
df <- data.frame(
  col1 = 1:3,
  col2 = letters[1:3]
)

df

# output
  col1 col2
1    1    a
2    2    b
3    3    c

# Convert to tibble
tbl <- as_tibble(df)

tbl
# A tibble: 3 × 2
   col1 col2 
  <int> <chr>
1     1 a    
2     2 b    
3     3 c   

In the above example, you can see that the tibble has better printing, enhanced output, and additional information to understand the data frame.

as_data_frame()

as_data_frame() function from the dplyr package in R is used for converting the matrix or list objects into the data frame.

As compared to tibble (output from as_tibble() function), the as_data_frame() function generates a standard base R data frame and lacks the features of tibble.

as_data_frame() function is deprecated in favor of as_tibble() function.

Example of converting a list into a data frame with as_data_frame() function:

# import packages
library(dplyr)  

# Create a list
lst <- list(
  col1 = 1:3,
  col2 = letters[1:3]
)

lst

# output
$col1
[1] 1 2 3

$col2
[1] "a" "b" "c"

# convert to data frame (this is deprecated)
df <- as_data_frame(lst)

df

# output
Warning message:
`as_data_frame()` was deprecated in tibble 2.0.0.
ℹ Please use `as_tibble()` (with slightly different semantics) to convert to a tibble, or
  `as.data.frame()` to convert to a data frame.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated. 
> 
> df
# A tibble: 3 × 2
   col1 col2 
  <int> <chr>
1     1 a    
2     2 b    
3     3 c  

tbl_df()

tbl_df() function from tibble package in R is used for converting the data frame or matrix objects into the tibble data frame.

Similar to as_data_frame(), the tbl_df() function is also deprecated in favor of as_tibble() function.

# import packages
library(dplyr) 

# Create a list
lst <- list(
  col1 = 1:3,
  col2 = letters[1:3]
)

lst

# output
$col1
[1] 1 2 3

$col2
[1] "a" "b" "c"

# convert list to tibble (this is deprecated)
df <- tbl_df(lst)

df

# output
Warning message:
`tbl_df()` was deprecated in dplyr 1.0.0.
ℹ Please use `tibble::as_tibble()` instead.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated. 
# A tibble: 3 × 2
   col1 col2 
  <int> <chr>
1     1 a    
2     2 b    
3     3 c  

In summary, the as_tibble() function should be used for manipulating the data frame instead of as_data_frame() and tbl_df() functions in the tidyverse ecosystem in R.