Contents

This is an R Markdown file.. Click the hyperlink for help.

In this article, we’ll present the tibble R package, developed by Hadley Wickham. The tibble R package provides easy to use functions for creating tibbles, which is a modern rethinking of data frames.

This is a compilation of notes containing examples on the use of tibble package in R.

To begin with, let us load the package

Create a new tibble

To create a new tibble from combining multiple vectors, use the function data_frame():

## Warning: `data_frame()` is deprecated as of tibble 1.1.0.
## Please use `tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## # A tibble: 4 x 4
##   name      age height married
##   <chr>   <dbl>  <dbl> <lgl>  
## 1 Nicolas    27    180 TRUE   
## 2 Thierry    25    170 FALSE  
## 3 Bernard    29    185 TRUE   
## 4 Jerome     26    169 TRUE

Note: Compared to the traditional data.frame(), the modern data_frame() : - never converts string as a factor - never changes the names of the variables - never creates new row names

Convert data as tibble

To convert traditional data as a tibble , use the function as_data_frame()[in tibble package], which works on data frames, lists, matrices and tables:

## [1] "data.frame"

Print the first 6 rows

##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          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.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Convert iris data to a tibble

## Warning: `as_data_frame()` is deprecated as of tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## [1] "tbl_df"     "tbl"        "data.frame"
## # 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

Note. In the case that you want to turn a tibble back to a data frame, use as.data.frame()

Advantages of tibble

  1. Tibbles have nice printing method that show only the first 10 rows and all the columns that fit on the screen. This is useful when you work on large data sets.
  2. When printed, the data type of each column is specified(see below):
  • : for double
  • : for factor
  • : for character
  • : for logical

It is also possible to change the default printing appearance as follows: - Change the maximum and minimum rows to print : options(tibble.print_max=20, tibble.print_min=6) - Always show all rows : options(tibble.print_max=inf) - Always show all columns : options(tibble.width=inf)

Summary

  • Create a tibble: data_frame()
  • Convert your data as a tibble: as_data_frame()
  • Change default printing appearance of a tibble: **options(tibble.print_max=20, tibble.print_min=6)