Tibbles

by Richard in 
r

I have to work with Excel files a lot, and converting them to csv is a pain, so I was recently looking for ways to import .xlsx files into R. After some searching, I found the excellent realXL package. It worked perfectly except for one small but horrifying thing; the data is imported as a “tibble” rather than a data frame.

I’m not quite sure why, but the thought of dealing with tibbles was not pleasant to me. I immediately tried to convert it to a data frame by using the function as.data.frame which, remarkably, worked. I think this may be the first time that I have been able to guess the name of an R function without looking it up in the documentation!

Impressed that the inventor of tibbles had been thoughtful enough to come up with a naming convention which is consistent with the rest of R, I decided to take a closer look, and discovered that the documentation on tibbles is rather excellent. It turns out that a tibble is a data frame, but it just has some annoying behaviours removed.

For example, if you try to create a data frame like this:

X1 <- data.frame(x=1:3, y=c("A", "B", "C"))
X1
  x y
1 1 A
2 2 B
3 3 C

it looks harmless enough, but it is unpleasantly surprising to discover that y is actually a factor, and will be treated by R as numeric under certain circumstances. This can really mess up calculations, and it is important when creating a data frame or importing data from a csv file to specify stringsAsFactors = F, unless you really want a factor variable.

X2 <- data.frame(x=1:3, y=c("A", "B", "C"), stringsAsFactors=F)
X2
  x y
1 1 A
2 2 B
3 3 C

The main thing which tibbles do is: not this. They also have a slightly nicer print method than the default for data frames.

library(tibble)
X3 <- tibble(x=1:3, y=c("A", "B", "C"))
X3
# A tibble: 3 x 2
      x     y
  <int> <chr>
1     1     A
2     2     B
3     3     C

I can see that tibbles would be a pleasant replacement for data frames in a future version of R, or a successor language. However, I do not like the name. The only similar word I have come across is “Tib”, which is the name of the ace of trumps in the old English card game of Gleek.

© Data Science Confidential 2017 All rights reserved.