When working with data in R, tibbles are often the data structure of choice due to their advantages over traditional data frames. However, one quirk that often puzzles new users is that, by default, tibbles do not print all of their rows to the console. This can be both convenient and frustrating. Convenient because it helps when you’re dealing with large datasets, but frustrating when you actually want to see all the data. This article aims to provide a comprehensive guide on how to print all rows of a tibble in R.
Table of Contents
- Introduction to Tibbles
- The Default Printing Behavior of Tibbles
- Methods for Printing All Rows
- Control the Printing Options
- Exporting Tibbles
- To CSV
- To Excel
- Tips and Caveats
1. Introduction to Tibbles
Tibbles are a modern take on data frames, but with some tweaks to make life a bit easier. They are provided in the
tidyverse set of packages but can also be invoked with the standalone
tibble package. A tibble retains the class of ‘data.frame’, meaning that any function expecting a data frame will also accept a tibble unless specifically restricted.
2. The Default Printing Behavior of Tibbles
By default, tibbles only print the first 10 rows and the columns that fit on the screen. This is done to conserve screen space and to provide a ‘glimpse’ of the data.
# Load the tibble package library(tibble) # Create a tibble my_tibble <- tibble(x = 1:100, y = 101:200) # Default print behavior my_tibble
3. Methods for Printing All Rows
3.1 The print( ) Function
You can use the
print() function and specify the
n argument to print all rows. Setting
n = Inf will print all rows regardless of the number.
print(my_tibble, n = Inf)
3.2 The as.data.frame( ) Function
Converting a tibble to a data frame and then printing it will also show all rows, because data frames do not have the same ‘glimpse’ behavior as tibbles.
3.3 The glimpse( ) Function
glimpse() function provides another way to look at the complete data, although it transposes the output to display it in a more compact, columnar format.
4. Control the Printing Options
4.1 options( )
You can use
options() to set the default number of rows to print for all tibbles in an R session:
options(tibble.print_max = Inf)
5. Exporting Tibbles
5.1 To CSV
You can easily write a tibble to a CSV file using the
write_csv() function from the
library(readr) write_csv(my_tibble, "my_tibble.csv")
5.2 To Excel
writexl package allows you to write a tibble directly to an Excel workbook.
library(writexl) write_xlsx(my_tibble, "my_tibble.xlsx")
6. Tips and Caveats
- Printing all rows is not recommended for extremely large datasets.
- Always be cautious when exporting large tibbles to file formats like CSV or Excel, as you could end up with very large files.
Printing all rows of a tibble can be done in various ways, from converting it to a data frame to using specific functions or setting global options. Each method has its advantages and drawbacks, and the best method for you will depend on your specific needs.