Understanding Tibble and the as_tibble() function

1. What is Tibble?

Tibble is one of the data structures for handling data in R and can be seen as a more useful extension of R's data frame (data.frame).

Original Korean article: Understanding Tibble and the as_tibble() function

Tibble is provided as part of the tidyverse package and is compatible with data frames. Tibble has nice-looking data output, is useful when processing partially large data, and is simpler when dealing with variable types or variable names.

1) Main features

  • Output: Tibble is highly readable when output to the console. First, you may want to show only 10 rows and not all columns.
  • Column data types: Tibble maintains column data types better. For example, character data remains character type.
  • Partial selection of columns and rows: Tibble is also more reliable when using [[]] or $. For example, requesting a column name that does not exist will return an error.

2) Creating Tibble

There are several ways to create a Tibble.

  • Create your own using the tibble() function:
library(tibble) my_tibble <- tibble(x = 1:5, y = 1, z = x ^ 2 + y)
  • Convert an existing data frame to the as_tibble() function:
my_data_frame <- data.frame(x = 1:5, y = 1, z = x ^ 2 + y) my_tibble <- as_tibble(my_data_frame)
  • Create it directly using the tibble() function: library(tibble) my_tibble <- tibble(x = 1:5, y = 1, z = x ^ 2 + y)
  • Convert an existing data frame to the as_tibble() function: my_data_frame <- data.frame(x = 1:5, y = 1, z = x ^ 2 + y) my_tibble <- as_tibble(my_data_frame)

3) Using Tibble

Tibble works much like a data frame, so it is compatible with most data frame functions.

# example example
my_tibble$x
# example example
my_tibble[1:2,]
# dplyr example
library(dplyr)
my_tibble %>% filter(x > 2)

4) Tibble add-on

Tibble also offers some additional features and options. For example, you can force data types and add metadata for rows and columns.

Tibble is easier to handle, more readable, and can effectively handle large data sets than traditional data frames.

tibble is a subclass of data frame (data.frame) used when handling data in R. tibble is part of the tidyverse, making data processing simpler and more efficient in a variety of ways. The as_tibble() function converts a given data object into a tibble object.

3) Check Tibble data type

To check the tibble data type, you must install the tibble package.

install.packages("tibble")

You can check tibble data with is_tibble. The output value of class(ti_iris) [1] “tbl_df” “tbl” “data.frame” indicates that the ti_iris object has multiple classes. In R, an object can have multiple classes, which reflects the object's inheritance structure.

  1. “tbl_df”: This indicates that ti_iris is a data frame in tibble format. tbl_df is a class defined in the tibble package.
  2. “tbl”: This class is the superclass of tbl_df and represents the basic characteristics of a tbl object. This usually appears together with tbl_df and is defined in the tibble package.
  3. “data.frame”: This indicates that ti_iris is also a regular R data frame by default. data.frame is one of the base classes in R.
is_tibble(ti_iris)
TRUE
class(ti_iris)
[1] "tbl_df"     "tbl"        "data.frame"

2. Understanding as_tibble() function

The as_tibble() function converts various data objects (e.g. data.frame, matrix, etc.) into tibble form. A tibble is similar to a data frame, but has some important differences. For example, tibble allows for cleaner data output and more flexibility in handling variable types.

The as_tibble() function is used to convert data into tibble format and supports various options. Below is a detailed description of the options:

1) x

The first argument x is the data you want to convert. This can take many forms: vectors, lists, data frames, matrices, etc.

as_tibble(data.frame(x = 1:3, y = 4:6))

2) .rows

The .rows option specifies the number or range of rows to load. This allows you to select only some rows from a large dataset.

as_tibble(iris, .rows = 1:5)

3) .name_repair

The .name_repair option specifies how to handle column names. This option can have the following values:

  • “minimal”: No modifications are made.
  • “unique”: Makes column names unique.
  • “universal”: Converts to a valid column name.
  • “check_unique”: Checks if column names are unique, otherwise raises an error.
as_tibble(data.frame(x = 1, x = 2), .name_repair = "unique")

4) .col_names (deprecated)

This option was used to specify column names in previous versions, but is now deprecated. Use .name_repair instead.

5) … (deprecated)

This is an option to accept additional arguments and is currently deprecated.

Example:

# .name_repairexample example example example example
as_tibble(data.frame(` ` = 1:3, x = c('a', 'b', 'c')), .name_repair = "universal")
# .rowsexample example example example example
as_tibble(mtcars, .rows = 1:5)

Combining these options allows you to perform a wide variety of data transformation tasks.

3. Main usage of as_tibble() function

1) Basic usage

  • as_tibble(x, …)

x : target data object … : additional optional arguments

2) Convert data frame to tibble

# tibble example example
library(tibble)
# example example example
df <- data.frame(name = c("Alice", "Bob", "Carol"),
                 age = c(30, 40, 50))
# as_tibbleexample example
df_tibble <- as_tibble(df)
# example example
print(df_tibble)

3) Convert matrix to tibble

# example example
mat <- matrix(1:6, nrow = 2)
# as_tibbleexample example
mat_tibble <- as_tibble(mat)
# example example
print(mat_tibble)

4) Convert list to tibble

For lists, each list element becomes a column of tibble.

# example example
lst <- list(name = c("Alice", "Bob"), age = c(30, 40))
# as_tibbleexample example
lst_tibble <- as_tibble(lst)
# example example
print(lst_tibble)

4. as_tibble() complex example

1) Overlapping data

The as_tibble function can also handle nested data structures, such as lists of lists.

# example example
nested_list <- list(
  meta = list(name = "sample", version = "1.0"),
  data = list(
    id = 1:3,
    value = c("a", "b", "c")
  )
)
# example example tibbleexample example
nested_tibble <- as_tibble(nested_list)
print(nested_tibble)

Here, nested_tibble has two columns, meta and data, and each column is again made up of a list.

2) Combination of data frame and list

The as_tibble function is also useful when some columns of the data frame are made up of lists.

# example example example example
df_with_list <- data.frame(
  id = 1:3,
  meta = I(list(
    list(name = "Alice", age = 30),
    list(name = "Bob", age = 40),
    list(name = "Carol", age = 50)
  ))
)
# as_tibbleexample example
tibble_with_list <- as_tibble(df_with_list)
print(tibble_with_list)

In this way, the as_tibble function can flexibly handle complex data structures along with various options. You can obtain more efficient results by utilizing the various features of this function in complex data analysis or preprocessing tasks.

To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).

Good article to read together

  • Text replacement str_replace, str_replace_all functions
  • str_squish function to remove unnecessary spaces
  • unnest_tokens() function
  • Execute PHP and R code in conjunction
  • Importance and usage of pipe operator %>%

Related Reading

FAQ

What is this article about?

This article is an English translation and global-reader adaptation of the Korean post “Understanding Tibble and the as_tibble() function.” It preserves the original article’s main explanation, examples, and practical context.

Why is it translated into English?

The English version helps global readers access Thinknote articles through English search keywords while keeping the Korean source available as the original reference.

Where can I read the original Korean version?

You can read the original Korean article here: https://www.thinknote.co.kr/r-tibble-as-tibble-guide/