tibble() is a nice way to create a tibble from scratch, and as_tibble() converts other data frames (e.g., data.frame and matrix) to a tibble. Below we’ll discuss how to create tibbles using these two functions; during the processing of creating tibbles, we’ll also discuss their important properties.
1. You can create a tibble in a similar way as with data.frame simply by specifying the name of the column and the associated cell values. Different from data.frame, a tibble when printed will nicely display the dataset dimension (row number × column number), and the types of each column, making it informative for downstream analysis.
# A tibble: 3 × 3 Name Age City <chr> <dbl> <chr> 1 Alice 25 New York 2 Bob 30 Los Angeles 3 Charlie 35 Chicago
A tibble has an official class of tbl_df**, as well as data.frame and tbl.
class(a)
Output:
[1] "tbl_df" "tbl" "data.frame"
2. You can include lists in a tibble column. This is helpful to do high-performance functional programming. (You cannot do this with data.frame.)
tibble(x =1:3, y =list(1:5, 1:10, 1:20))
Output:
# A tibble: 3 × 2 x y <int> <list> 1 1 <int [5]> 2 2 <int [10]> 3 3 <int [20]>
3. tibble() builds columns sequentially. When defining a column, you can refer to columns created earlier; e.g., column C is created based on prior-defined A and B. (You cannot do this with data.frame.)
tibble(A =1:3, B =c(1, 3, 6), C = A^2+ B)
Output:
# A tibble: 3 × 3 A B C <int> <dbl> <dbl> 1 1 1 2 2 2 3 7 3 3 6 15
5. Only columns of length one can be recycled. In the code below, for instance, you cannot recycle B = c(1, 2) three times to create a total of six rows. This limit is designed so because recycling vectors of longer length is a frequent source of bugs. In comparison, data.frame can recycle vectors of longer length(e.g., data.frame(A = 1:6, B = c(1, 2)) recycles B = c(1, 2) three times.)
tibble(A =1:6, B =c(1, 2))# Error:# ! Tibble columns must have compatible sizes.# • Size 6: Existing data.# • Size 2: Column `B`.# ℹ Only values of size one are recycled.
data.frame(A =1:6, B =c(1, 2))
Output:
A B 1 1 1 2 2 2 3 3 1 4 4 2 5 5 1 6 6 2
6. Use as_tibble() to convert a data.frame to a tibble.
as_tibble(mtcars) # convert 'data.frame' mtcars dataset to a tibble