Table of Contents
To convert a Tibble to a data frame in R, you can use the “as.data.frame()” function. This function will take the Tibble object and transform it into a data frame structure. An example code would be:
tibble_df
Convert Tibble to Data Frame in R (With Example)
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
Cite this article
stats writer (2024). How can I convert a Tibble to a data frame in R? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-tibble-to-a-data-frame-in-r-can-you-provide-an-example/
stats writer. "How can I convert a Tibble to a data frame in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-tibble-to-a-data-frame-in-r-can-you-provide-an-example/.
stats writer. "How can I convert a Tibble to a data frame in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-tibble-to-a-data-frame-in-r-can-you-provide-an-example/.
stats writer (2024) 'How can I convert a Tibble to a data frame in R? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-tibble-to-a-data-frame-in-r-can-you-provide-an-example/.
[1] stats writer, "How can I convert a Tibble to a data frame in R? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert a Tibble to a data frame in R? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
