Convert Tibble to dataframe and vice-versa
Introduction
We have already covered the basics of tibble and dataframes here. We know how to create dataframe using tibble, we will now convert tibble to data.frame and vice-versa. This might be useful when we encounter compatibility issues.
Procedure
We will be working with the following tibble:
# package for creating tibble
library(tibble)
# create tibble
basic_tibble <- tibble(
Name = c("Anthony Stark", "Steve Rogers","Bruce Banner","Carol Denvers"),
Age = c(45,115,47,80),
`Alias Name` = c("Ironman","Captain America","Hulk","Captain Marvel")
)
# view tibble
View(basic_tibble)
The tibble is as follows:
And we will be working with the following data.frame:
# create data.frame
basic_dataframe <- data.frame(
Name = c("Anthony Stark", "Steve Rogers","Bruce Banner","Carol Denvers"),
Age = c(45,115,47,80),
`Alias Name` = c("Ironman","Captain America","Hulk","Captain Marvel")
)
# view data.frame
View(basic_dataframe)
The data.frame is as follows:
We will convert basic_tibble to data.frame and basic_dataframe to tibble using the as.data.frame(…) and as_tibble(..) functions respectively.
Code
The basic_dataframe is converted to tibble and stored in dataframe_to_tibble.
# refer procedure for definition of basic_dataframe
dataframe_to_tibble <- as_tibble(basic_dataframe)
View(dataframe_to_tibble)
The basic_tibble is converted to data.frame and stored in tibble_to_dataframe.
# refer procedure for definition of basic_tibble
tibble_to_dataframe <- as.data.frame(basic_tibble)
View(tibble_to_dataframe)
Conclusion
Thus we have successfully converted tibble to data.frame and vice-versa.
References
- https://r4ds.had.co.nz/