# Tibbles are part of the tibble package, which is included in tidyverse# You can install/load tibble directlyif(!require(tibble)) {install.packages("tibble");require(tibble);}
Loading required package: tibble
# For more info see:## help(package="tibble")
9.2 What are Tibbles?
Tibbles are a modern reimagining of R’s traditional data.frame. They are designed to make working with data frames easier and more consistent. Here’s how to create a basic tibble:
9.2.1 tibble() function is similar to data.frame() function
Create a tibble with the tibble function. It’s use is very similar to how you would use the data.frame function to create a dataframe (we’re assuming that you’re familiar with creating dataframes in R)
# width argument to print specifies the number of characters that should# be printed in the widest row. In effect, this limits the number of columns# being output to those columns that fit in the specified width.print(health_data, width =75)
# data.frames can have row namesdf_rownames =data.frame(x =1:3,y = letters[1:3],row.names =c("row1", "row2", "row3"))df_rownames
x y
row1 1 a
row2 2 b
row3 3 c
# Tibbles don't support row names# If you convert a data.frame with row names to a tibble,# the row names become a regular column called 'rowname'as_tibble(df_rownames, rownames ="id")
# A tibble: 3 × 3
id x y
<chr> <int> <chr>
1 row1 1 a
2 row2 2 b
3 row3 3 c
9.5 Creating Tibbles
You can create tibbles in several ways:
# Using tibble()t1 =tibble(x =1:5,y = x *2, # Note: you can refer to columns created earlierz = letters[1:5])t1
# A tibble: 5 × 3
x y z
<int> <dbl> <chr>
1 1 2 a
2 2 4 b
3 3 6 c
4 4 8 d
5 5 10 e
9.6 creating tibble row by row using tribbles
While reading the raw code for creating a dataframe or a tibble, it can be challenging to visualize what the actual dataframe/tibble will look like. This is because when typing the data into the code, each column is typed horrizontally instead of vertically. For example:
# Using tribble() for transposed input
# Useful for small, manual data entry
stuff = tribble(
col1 = c("a", "b", "c"),
col2 = c( 1, 2, 3)
col3 = c(TRUE, FALSE, TRUE))
# The code above lays out columns horizontally.
# The actual dataframe displays columns vertically.
stuff
A “tribble” (i.e. TRansposed tIBBLE) is just a different way of typing the code that becomes a tibble. Each column heading is prefixed with a tilde (~). The columns can be laid out vertically in the code, making the code much more readable. See the example below.
# Using tribble() for transposed input
# Useful for small, manual data entry
stuff = tribble(
~col1, ~col2, ~col3,
"a", 1, TRUE,
"b", 2, FALSE,
"c", 3, TRUE
)
# The following looks much more similar to the code that created it.
stuff
9.7 Converting Between Tibbles and data.frames
# Convert data.frame to tibbledf =data.frame(x =1:3,y = letters[1:3])tbl =as_tibble(df)# Convert tibble back to data.framedf_again =as.data.frame(tbl)# Check classesclass(tbl)
[1] "tbl_df" "tbl" "data.frame"
class(df_again)
[1] "data.frame"
9.8 Other differences between tibbles and dataframes