What are tibbles?
Precursors
Tribblemaking
Tibbles vs Data Frames
Disadvantages
To summarise..
Let’s get something straight, there isn’t really any trouble with tibbles. I’m hoping you’ve noticed this is a play on 1967 Star Trek episode, “The Trouble with Tribbles”. I’ve recently got myself a job as a Data Scientist, here, at Jumping Rivers. Having never come across tibbles until this point, I now find myself using them in nearly every R script I compose. Be that your timeless standard R script, your friendly Shiny app or an analytical Markdown document.
What are tibbles?
Presumably this is why you came here, right?
Tibbles are a modern take on data frames, but crucially they are still data frames. Well, what’s the difference then? There’s a quote I found somewhere on the internet that decribes the difference quite well;
“keeping what time has proven to be effective, and throwing out what is not”.
Basically, some clever people took the classic data.frame(), shook it til the ineffective parts fell out, then added some new, more appropriate features.
Precursors
# The easiest way to get access is to isstall the tibble package.
install.packages(&quot;tibble&quot;)
# Alternatively, tibbles are a part of the tidyverse and hence installing the whole tidyverse will give you access.
install.packages(&quot;tidverse&quot;)
# I am just going to use tibble.
library(tibble)
Tribblemaking
There are three ways to form a tibble. It pretty much acts as your friendly old pal data.frame() does. Just like standard data frames, we can create tibbles, coerce objects into tibbles and import data sets into R as a tibble. Below is a table of the traditional data.frame() commands and their respective tidyverse commands.
Formation Type
Data Frame Commands
Tibbles Commands
Creation
data.frame()
data_frame() tibble() tribble()
Coercion
as.data.frame()
as_data_frame() as_tibble()
Importing
read.*()
read_delim() read_csv() read_csv2() read_tsv()
Let’s take a closer look…
1) Creation.
Just as data.frame() creates data frames,tibble(), data_frame() and tribble() all create tibbles.
Standard data frame.
data.frame(a = 1:5, b = letters[1:5]) ## a b
## 1 1 a
## 2 2 b
## 3 3 c
## 4 4 d
## 5 5 e
A tibble using tibble() (identical to using data_frame).
tibble(a = 1:5, b = letters[1:5])
## # A tibble: 5 x 2
## a b ## ## 1 1 a ## 2 2 b ## 3 3 c ## 4 4 d ## 5 5 e
A tibble using tribble().
tribble( ~a, ~b, #---|----
1, &quot;a&quot;, 2, &quot;b&quot;)
## # A tibble: 2 x 2
## a b ## ## 1 1.00 a ## 2 2.00 b
Notice the odd one out? tribble() is different. It’s a way of laying out small amounts of data in an easy to read form. I’m not too keen on these, as even writing out that simple 2 x 2 tribble got tedious.
2) Coercion.
Just as as.data.frame() coerces objects into data frames, as_data_frame() and as_tibble() coerce objects into tibbles.
df = data.frame(a = 1:5, b = letters[1:5])
as_data_frame(df)
## # A tibble: 5 x 2
## a b ## ## 1 1 a ## 2 2 b ## 3 3 c ## 4 4 d ## 5 5 e
as_tibble(df)
## # A tibble: 5 x 2
## a b ## ## 1 1 a ## 2 2 b ## 3 3 c ## 4 4 d ## 5 5 e
You can coerce more than just data frames, too. Objects such as lists, matrices, vectors and single instances of class are convertible.
3) Importing.
There’s a few options to read in data files within the tidyverse, so we’ll just compare read_csv() and its representative data.frame() pal, read.csv(). Let’s take a look at them. I have here an example data set that I’ve created in MS Excel. You can download/look at this data here. To get access to this function you’ll need the readr package. Again this is part of the tidyverse so either will do.
library(&quot;readr&quot;)
url = &quot;https://gist.githubusercontent.com/theoroe3/8bc989b644adc24117bc66f50c292fc8/raw/f677a2ad811a9854c9d174178b0585a87569af60/tibbles_data.csv&quot;
tib = read_csv(url)
## Parsed with column specification:
## cols(
## `