Tibbles and readr

A tibble is the modern tidyverse version of a data.frame. A tibble is a data.frame, and so can be used in the same way. But it comes with more powerful features and removes inconsistent and confusing behaviour.

In the same way, readr provides modern tidyverse replacements for R’s standard reading functions. readr provides read_csv, which is a better way of reading csv files than R’s standard read.csv.

Let’s now use the tidyverse to read_csv the dataset Anatomical Data from Domestic Cats into a tibble. The first thing you will notice is that the tidyverse has printed out some useful information.

Error in library(tidyverse): there is no package called 'tidyverse'
# Did you load tidyverse? library(tidyverse)

cats <- read_csv("https://raw.githubusercontent.com/Bristol-Training/intermediate-r/refs/heads/main/data/cats.csv")
Error in read_csv("https://raw.githubusercontent.com/Bristol-Training/intermediate-r/refs/heads/main/data/cats.csv"): could not find function "read_csv"

This is telling you that read_csv found three columns; Sex, which is treated as a columns of strings (characters), and BodyWeight and HeartWeight, which are both treated as columns of floating point numbers (doubles).

Next, if you type cats and press return you will see

cats
Error: object 'cats' not found

that the tibble summarises itself to the screen. This makes it much easier to quickly look at some data without it overflowing your console.

As a tibble is a data.frame, you can use the same methods of accessing data, e.g.

cats$BodyWeight
Error: object 'cats' not found
cats[1,]
Error: object 'cats' not found
NoteExercise

Load the cats data set into a tibble using read_csv. Use the mean and max functions to calculate the mean and max body weight and heart weight of the cats.

Note that according to this dataset description the body weight is in kilograms and the heart weight is in grams. As is normal for real-world data, there is little consistency in units.

library(tidyverse)
Error in library(tidyverse): there is no package called 'tidyverse'
cats <- read_csv("https://raw.githubusercontent.com/Bristol-Training/intermediate-r/refs/heads/main/data/cats.csv")
Error in read_csv("https://raw.githubusercontent.com/Bristol-Training/intermediate-r/refs/heads/main/data/cats.csv"): could not find function "read_csv"
mean_body_weight <- mean(cats$BodyWeight)
Error: object 'cats' not found
max_body_weight <- max(cats$BodyWeight)
Error: object 'cats' not found
cat( sprintf("Body weight: mean = %.2f kg, max = %.2f kg\n",
             mean_body_weight, max_body_weight) )
Error: object 'mean_body_weight' not found
cat( "Heart Weight: mean = ",
      mean(cats$HeartWeight) %>% round(digits=2),
      "g, max =",
      max(cats$HeartWeight) %>% round(digits=2),
      "g\n")
Error in mean(cats$HeartWeight) %>% round(digits = 2): could not find function "%>%"