Lecture 7: Tibbles
August 22, 2026
๐ Last lecture we studied the following topics:
๐ Today we will move on to looking at Tibble objects and functions from the tidyverse library, specifically:
|>dplyr operators from the tidyverse package
We have seen that dataframes are helpful but come with some frustrating drawbacks:
NA values.Error in data.frame(x = 1:3, y = list(1:5, 1:10, 1:20)): arguments imply differing number of rows: 3, 20
The tidyverse package provides a solution in the form of tibble() objects.
First, make sure you have the tidyverse library loaded:
We can use the tibble() function to convert a dataframe into a tibble.
๐ There are lots of helpful arguments (such as .name_repair above) allowing us to modify dataframes to be easier to manipulate โ look them up with ?tibble().
# A tibble: 60 ร 4
Cult Date HeadWt VitC
<fct> <fct> <dbl> <int>
1 c39 d16 2.5 51
2 c39 d16 2.2 55
3 c39 d16 3.1 45
4 c39 d16 4.3 42
5 c39 d16 2.5 53
6 c39 d16 4.3 50
7 c39 d16 3.8 50
8 c39 d16 4.3 52
9 c39 d16 1.7 56
10 c39 d16 3.1 49
# โน 50 more rows
Sequential Column Evaluation
Tibbles define columns sequentially:
Arithmetic
Tibbles do not support arithmetic across columns:
Recycling
Tibble recycling is extremely strict:
We often wish to apply one function after another after another when performing computations, a process known as functional composition.
Example: Say we wish to find which (numeric) column in the iris dataset has the highest mean.
๐ Here we first used the function mean(), which lies inside apply(), which is nested inside the function which.max() to get the desired result.
Nesting functions works fine but can quickly become very difficult to read.
The pipe operator |> allows us to chain (compose) several functions together in a clear and easy to read way. The pipe operator passes the left-hand side item as the first argument to the right-hand side function and returns the result.
This is by no means limited to being applied once!
Example: We can rewrite our previous function with pipe operators.
๐ The pipe operator is particularly helpful when we begin manipulating tibble objects using the dplyr functions in the following section.
03:00
Consider the following numerical vector:
The following nested function:
log(...)diff(..., lag = 1)exp(...)round(..., digits = 1)Write the equivalent function using the pipe |> operator.
We chain log(), diff(), exp() and round() together using the pipe operator:
๐ If we are calling a function but the pipe has already defined the first input, we still need the parentheses but leave them blank.
dplyr FunctionsThe dplyr functions are designed to interface easily with tibble data. The functions are loaded via the tidyverse package.
The functions are:
select() โ select specific columns;filter() โ filter rows based on logical conditions;mutate() โ define new columns or redefine existing columns;summarise() โ creates a new data frame of summary statistics of data grouped by a chosen variable; andarrange() โ orders tibble rows based on a selected column.These are often referred to as the โverbsโ of dplyr because they describe the action we are taking on the data.
select() FunctionThe select() function allows us to choose specific columns of a tibble object using either column names or indices.
By Name
Select using column names:
select() โ Deselecting & PatternsDeselecting
Select which columns to remove:
filter() FunctionThe filter() function allows us to filter rows based on logical conditions satisfied by each row.
Single Condition
Return rows that have Date of either "d20" or "d21":
Multiple Conditions
Return rows with (i) Cult of "c39", (ii) Date of "d16", and (iii) HeadWt between 2.8 and 3.3:
filter() and select()05:00
We shall use the painters dataframe from the MASS package, loaded into our environment using the following code:
painters dataframe into a tibble and save it to a new object called painters_tib.select() and filter() functions and the pipe operator |> to obtain a tibble object with columns for School, Drawing and Expression containing all painters from School A with Drawing and Expression scores above 5.filter() and select()We convert painters to a tibble, then use select() and filter() to obtain the required columns and rows:
# A tibble: 7 ร 3
School Drawing Expression
<fct> <int> <int>
1 A 16 14
2 A 13 7
3 A 16 8
4 A 16 14
5 A 17 8
6 A 16 6
7 A 18 18
mutate() FunctionNew Columns
Take the cabbages tibble we defined earlier. If we want to add a new column named Color we can do so using the mutate() function:
New Columns from Existing
Alternatively we can define a new column using data from existing columns. For example, we can define a new column called VitCPer detailing the VitC per HeadWt.
mutate() โ Changing & Removing ColumnsChanging Existing Columns
mutate() can also be used to change existing columns by simply stating the name of the column you wish to change.
For example, say we discover that due to equipment failure, the HeadWt variable should actually be 1.6 times bigger for all observations.
One problem with reading data into R is that data can come in a variety of different formats and file types (some easy to use, some challenging).
R has lots of functionality for dealing with these subtleties, but most problems can be reduced by checking a few key things:
data in this folder to store all your data files.On Canvas you should be able to see a module named Data.
For this lecture we will be using the following three files:
class.txtcustomers-100.csvSampleData.xlsxIf you are working along, save all three files to the data folder within your current working directory.
.txt Files.txt Files?A .txt file is a plain text file that can be opened in any text editor (e.g. Notepad, TextEdit, etc.) and contains data in a tabular format.
To load .txt files we use the function read.table():
.txt Files โ Key Arguments.txt file had a header, so we specified header = TRUE.sep = " "..csv Files.csv Files?A .csv file is a comma-separated value file (similar to an Excel spreadsheet but with fewer formatting options) that stores tabular data as plain text, with commas separating each value.
To load .csv files we use the function read.csv():
Index Customer.Id First.Name Last.Name
1 1 DD37Cf93aecA6Dc Sheryl Baxter
2 2 1Ef7b82A4CAAD10 Preston Lozano
3 3 6F94879bDAfE5a6 Roy Berry
4 4 5Cef8BFA16c5e3c Linda Olsen
5 5 053d585Ab6b3159 Joanna Bender
6 6 2d08FB17EE273F4 Aimee Downs
.csv Files โ Key Argumentsread.csv() assumes the file has a header row and is comma-separated by default, so unlike read.table() we donโt need to specify header or sep arguments..xlsx Files.xlsx Files?An .xlsx file is an Excel spreadsheet that may contain multiple sheets of tabular data.
To load .xlsx files we use the function read_excel() from the readxl package:
# A tibble: 6 ร 7
OrderDate Region Rep Item Units `Unit Cost` Total
<dttm> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 2021-01-06 00:00:00 East Jones Pencil 95 1.99 189.
2 2021-01-23 00:00:00 Central Kivell Binder 50 20.0 999.
3 2021-02-09 00:00:00 Central Jardine Pencil 36 4.99 180.
4 2021-02-26 00:00:00 Central Gill Pen 27 20.0 540.
5 2021-03-15 00:00:00 West Sorvino Pencil 56 2.99 167.
6 2021-04-01 00:00:00 East Jones Binder 60 4.99 299.
.xlsx Files โ Key Argumentssheet = 2 to tell read_excel() which sheet to read from.06:00
You will find on Canvas the dataset faithful.csv, which you will need for this final exercise.
faithful.csv and put it in the correct directory.faithful using the function read.csv().faithful to a tibble.scaled which contains the eruptions divided by waiting time of Old Faithful.We load the data, convert it to a tibble, then use mutate() and filter() to compute the scaled column and select the required rows:
๐ค Today we looked into:
.txt.csv.xlsx๐คฉ Next class we will study: