Printing Tibbles

Keywords: #tibble

A tibble is a modern and simple data.frame for containing our data set. The main feature is that the data visualization adapts to the available space of our window. The two basic functions to show the data set are:

  • print() - to show the data set
  • glimpse() - to show the data set transposing rows by columns

To use tibbles we need to load the package tibble, or the collection of R packages tidyverse.

library (tidyverse)

Let’s see how the results of these two functions for the iris data set.

data(iris)
iris %>% as_tibble() %>% print()
## # A tibble: 150 x 5
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            1.4         0.2 setosa 
##  3          4.7         3.2          1.3         0.2 setosa 
##  4          4.6         3.1          1.5         0.2 setosa 
##  5          5           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # … with 140 more rows
iris %>% as_tibble() %>% glimpse()
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…

As we have said, tibble, print(), and glimpse() adapt to the available space of the screen; however, we can force their behavior. For instance, we can define the number of rows to show with the parameter n.

iris %>% as_tibble() %>% print(n = 3) # show only three rows
## # A tibble: 150 x 5
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
## 1          5.1         3.5          1.4         0.2 setosa 
## 2          4.9         3            1.4         0.2 setosa 
## 3          4.7         3.2          1.3         0.2 setosa 
## # … with 147 more rows

We can also define the width of text output by the parameter width. If we wanto to show all columns, we define width=Inf.

iris %>% as_tibble() %>% print(n = 3, width = 55) # show only 55 characters in the rows
## # A tibble: 150 x 5
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
##          <dbl>       <dbl>        <dbl>       <dbl>
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3            1.4         0.2
## 3          4.7         3.2          1.3         0.2
## # … with 147 more rows, and 1 more variable:
## #   Species <fct>
iris %>% as_tibble() %>% glimpse(width = 40) # show only 40 characters in the rows
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.…
## $ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.…
## $ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.…
## $ Species      <fct> setosa, setosa, s…

Finally, we can define the umber of extra columns to print abbreviated information for with the parameter n_extra. In our case, as we don’t have so many columns, the result is the same.

iris %>% as_tibble() %>% print(n = 3, n_extra = 1) # show only 1 extra columns
## # A tibble: 150 x 5
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
## 1          5.1         3.5          1.4         0.2 setosa 
## 2          4.9         3            1.4         0.2 setosa 
## 3          4.7         3.2          1.3         0.2 setosa 
## # … with 147 more rows