![]() |
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.
In R there are two ways by which we can convert Tibble to Data Frame.
Using as. data.frame() functionIn 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
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" Using data.frame() functionIn this approach we will utilize The data.frame() function to convert tibble to a data frame.Usually this function is used to create data frames in R. This function can combine vectors, matrices, or other data frames into a single data frame. below example we have created a sample tibble which we have passed to The data.frame() function as argument. This will result into conversion of tibble into a data frame. R
Output: A tibble: 5 × 3 Name Age ID <chr> <dbl> <chr> 1 john 32 a 2 smith 25 b 3 virat 30 c 4 rohit 29 d 5 ayyer 20 e [1] "tbl_df" "tbl" "data.frame" Name Age ID 1 john 32 a 2 smith 25 b 3 virat 30 c 4 rohit 29 d 5 ayyer 20 e [1] "data.frame" Below code converts the combined Tibbles and Vectors to data frame. R
Output: A B C
1 1 apple cat
2 2 banana dog
3 3 cherry elephant
In below example we will convert a combined Tibble and matrix to a data frame. R
Output: A B X1 X2
1 1 apple 4 7
2 2 banana 5 8
3 3 cherry 6 9
ConclusionConverting a |
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |