Tibbles are not new to us. In Section 1.5, we learned that tables of data come in different types (e.g., matrices, data frames, and tibbles). Since then, we have been using mostly tibbles without reflecting on them. For instance, in Chapter 3 on Transforming data we used tibbles as the inputs of dplyr commands (e.g.,
summarise), which then returned modified tibbles as their outputs. Similary, the chapters on Visualizing data (Chapter 2) and EDA (Chapter 4) used tibbles as an input to
This chapter will explicate some features of tibbles that we may not have noticed before and introduce functions of the tibble package (Müller & Wickham, 2019), which provides additional ways of creating the fundamental data structure of the tidyverse (Wickham, Averick, et al., 2019).
After working through this chapter, you should be able to use tibble commands to:
- turn rectangular data into a tibble;
- turn vectors of variables into (the columns of) a tibble;
- turn cases into (the rows of) a tibble;
- enter some data into a tibble; and
- understand some limitations of rectangular data.
5.1.2 Data used
In this chapter, we will mostly use data provided by the datasets package that comes with R. In contrast to data included in the tidyverse, these datasets typically are not stored as a tibble, but can easily be converted into one. In addition to creating tibbles out of pre-existing data, we will also learn to create new tibbles from scratch.
5.1.3 Getting ready
This chapter formerly assumed that you have read and worked through Chapter 10: Tibbles of the r4ds textbook (Wickham & Grolemund, 2017). It now can be read by itself, but reading Chapter 10 of r4ds is still recommended.
Please do the following to get started:
Structure your document by inserting headings and empty lines between different parts. Here’s an example how your initial file could look:
Create an initial code chunk below the header of your
.Rmdfile that loads the R packages of the tidyverse (and see Section F.3.3 if you want to get rid of the messages and warnings of this chunk in your HTML output).
Save your file (e.g., as
05_tibbles.Rmdin the R folder of your current project) and remember saving and knitting it regularly as you keep adding content to it.
Müller, K., & Wickham, H. (2019). tibble: Simple data frames. Retrieved from https://CRAN.R-project.org/package=tibble
Wickham, H., Averick, M., Bryan, J., Chang, W., McGowan, L. D., François, R., … Yutani, H. (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686. https://doi.org/10.21105/joss.01686
Wickham, H., & Grolemund, G. (2017). R for data science: Import, tidy, transform, visualize, and model data. Retrieved from http://r4ds.had.co.nz