as_tibble() in R: How to Coerce Lists and Matrices to data frames

To coerce a list, matrix to tibble in R, use the as_tibble() function. If you face could not find function “as_tibble” that means you need to install either tidyverse package or tibble package. It will resolve the issue.

as_tibble() in R

The as_tibble() is a new S3 generic function in R that coerce lists and matrices to data frames. The as_tibble() method turns an existing R object, such as a data frame or matrix, into a Tibble, a data frame with class tbl_df.

This contrasts with the tibble() method, which builds the tibble from the individual columns.

It has two more variants.

  1. as_tibble_row()
  2. as_tibble_col()

as_tibble_row in R

To convert a vector to tibble in R, use the as_tibble_row() method. The user input should be a vector. For example, vectors of dates are not supported yet. If the input is a list, all elements must have length one.

as_tibble_col in R

To convert a vector to a tibble with one column in R, use the as_tibble_col() function. The as_tibble() function for vectors is superseded as of R version 3.0.0, prefer the more expressive as_tibble_row() and as_tibble_col() variants for new code.

Syntax

as_tibble(
 x,
 validate = NULL,
 ...,
 .rows = NULL,
 .name_repair = c("check_unique", "unique", "universal", "minimal"),
 rownames = pkgconfig::get_config("tibble::rownames", NULL)
)

Parameters

x: This can be any R object like list, matrix, or data frame.

.rows: They are the number of rows, helpful to create a 0-column tibble or just as an additional check.

rownames: It suggests how to treat existing row names of a data frame or matrix.

  1. NULL: Remove row names. This is the default.
  2. NA: Keep the rownames.
  3. A string: It is the name of a new column. Existing rownames are transferred into this column, and the row.names attribute is deleted.

validate: For compatibility only, do not use for new code.

.name_repair: It is used to treat problematic column names.

  1. minimal“: No name repair or checks beyond basic existence.
  2. unique“: Make sure names are unique and not empty.
  3. check_unique“: (default value), no name repair, but check they are unique.
  4. universal“: Make the names unique and syntactic.

How to Create a tibble in R

To create a tibble from the matrix, use the as_tibble() method. To work with the as_tibble() method, you should import the dplyr library in your program.

library("dplyr")

If you have not installed it yet, please install it.

The next step is to create a matrix. We will create a matrix using the matrix() function and then use the colnames() function to assign the column name to the matrix.

library("dplyr")

m <- matrix(1:20, ncol = 5)

colnames(m) <- c("a", "b", "c", "d", "e")

Now, to convert a matrix into a data frame, use the as_tibble() method.

library(dplyr)

m <- matrix(1:20, ncol = 5)

colnames(m) <- c("a", "b", "c", "d", "e")

df <- as_tibble(m)

df

Output

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

 filter, lag

The following objects are masked from ‘package:base’:

 intersect, setdiff, setequal, union

# A tibble: 4 x 5
   a     b     c     d     e
 <int> <int> <int> <int>  <int>
1  1     5     9    13    17
2  2     6    10    14    18
3  3     7    11    15    19
4  4     8    12    16    20

You can see that we have coerced the matrix into a data frame.

R as_tibble_row() Function Example

To work with as_tibble_row() and as_tibble_col() method, import the tibble package. If you have not installed then please install the tibble package.

After installing, import in your program using the following code.

library(tibble)

Now, convert a vector to a tibble with one row using the as_tibble_row() function.

library(tibble)

dt <- as_tibble_row(c(a = 19, b = 21))

dt

Output

# A tibble: 1 x 2
    
    a        b
  <dbl>    <dbl>
1  19       21

R as_tibble_col() Function Example

To convert a vector to a tibble with one column in R, use the as_tibble_col(). To work with the as_tibble_col() method, import the tibble package. If you have not installed then please install the tibble package.

After installing, import in your program using the following code.

library(tibble)

Now, write the following code.

library(tibble)

dc <- as_tibble_col(1:3)

dc

Output

# A tibble: 3 x 1
  value
  <int>
1   1
2   2
3   3

That is it for as_tibble() function in R.

Leave a Comment