A tibble is a data frame in R that has a refined print method that only shows the first 10 rows of a data frame.
This makes it much easier to work with large data and prevents R from attempting to display every row if you accidently print a large data frame in the console.
However, occasionally you may want to convert a tibble to a data frame.
You can use the following syntax to do so:
my_df <- as.data.frame(my_tibble)
The following example shows how to use this syntax in practice.
Example: Convert Tibble to Data Frame in R
Suppose we use the read_csv() function to read a CSV file into R:
library(tidyverse)
#import CSV file into tibble
my_tibble <- read_csv('my_data.csv')
#view tibble
print(my_tibble)
# A tibble: 7 x 3
points assists rebounds
1 24 4 8
2 29 4 8
3 33 6 5
4 34 7 5
5 20 5 9
6 18 9 12
7 19 10 10
#view class
class(my_tibble)
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
By default, the read_csv() function imports the CSV file as a tibble.
However, we can use the following syntax to convert this tibble to a data frame:
#convert tibble to data frame
my_df <- as.data.frame(my_tibble)
#view class of my_df
class(my_df)
[1] "data.frame"
We can see that the tibble has been successfully converted to a data frame.
We can also confirm that the data frame contains the exact same values as the tibble:
#view data frame
print(my_df)
points assists rebounds
1 24 4 8
2 29 4 8
3 33 6 5
4 34 7 5
5 20 5 9
6 18 9 12
7 19 10 10
The values in the data frame are identical to those in the tibble.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
How to Print All Rows of a Tibble in R
How to Convert Data Frame to Matrix in R