tibble (version 1.4.2)

as_tibble: Coerce lists and matrices to data frames

Description

as.data.frame() is effectively a thin wrapper around data.frame, and hence is rather slow (because it calls data.frame() on each element before cbinding together). as_tibble is a new S3 generic with more efficient methods for matrices and data frames.

Usage

as_tibble(x, ...)

# S3 method for tbl_df as_tibble(x, ..., validate = FALSE, rownames = NULL)

# S3 method for data.frame as_tibble(x, validate = TRUE, ..., rownames = NA)

# S3 method for list as_tibble(x, validate = TRUE, ...)

# S3 method for matrix as_tibble(x, ..., rownames = NULL)

# S3 method for table as_tibble(x, n = "n", ...)

# S3 method for NULL as_tibble(x, ...)

# S3 method for default as_tibble(x, ...)

Arguments

x

A list. Each element of the list must have the same length.

...

Other arguments passed on to individual methods.

validate

When TRUE, verifies that the input is a valid data frame (i.e. all columns are named, and are 1d vectors or lists). You may want to suppress this when you know that you already have a valid data frame and you want to save some time, or to explicitly enable it if you have a tibble that you want to re-check.

rownames

If NULL, remove row names (default for matrices, may become default for data frames in the future). If NA, keep row names (current default for data frames). Otherwise, the name of the new column that will contain the existing row names.

n

Name for count column, default: "n".

Details

This is an S3 generic. tibble includes methods for data frames (adds tbl_df classes), tibbles (returns unchanged input), lists, matrices, and tables. Other types are first coerced via as.data.frame() with stringsAsFactors = FALSE.

as_data_frame and as.tibble are aliases.

Examples

Run this code
# NOT RUN {
l <- list(x = 1:500, y = runif(500), z = 500:1)
df <- as_tibble(l)

m <- matrix(rnorm(50), ncol = 5)
colnames(m) <- c("a", "b", "c", "d", "e")
df <- as_tibble(m)

# as_tibble is considerably simpler than as.data.frame
# making it more suitable for use when you have things that are
# lists
# }
# NOT RUN {
if (requireNamespace("microbenchmark", quiet = TRUE)) {
  l2 <- replicate(26, sample(letters), simplify = FALSE)
  names(l2) <- letters
  microbenchmark::microbenchmark(
    as_tibble(l2, validate = FALSE),
    as_tibble(l2),
    as.data.frame(l2)
  )
}

if (requireNamespace("microbenchmark", quiet = TRUE)) {
  m <- matrix(runif(26 * 100), ncol = 26)
  colnames(m) <- letters
  microbenchmark::microbenchmark(
    as_tibble(m),
    as.data.frame(m)
  )
}
# }

Run the code above in your browser using DataLab