Skip to contents

A faster function to replace as.data.frame(). Returns a plain data.frame.

Usage

as_df(x)

# S3 method for class 'list'
as_df(x)

# S3 method for class 'matrix'
as_df(x)

# S3 method for class 'group_df'
as_df(x)

# S3 method for class 'nest_df'
as_df(x)

# Default S3 method
as_df(x)

Arguments

x

The data object to be converted to a data.frame.

Value

data.frame

Details

Rownames are always integers. Speed benefits are mainly seen for converting lists. as_df.matrix() is provided, but as.data.frame() should probably be used instead as there is only a small speed benefit. as_df.default() checks if the object is matrix-like (row and column dimensions exist) but the object just so happens to not have class matrix (an unusual scenario, mainly for use as a helper within this package). as_df() supports creating list-column dataframes, although care is needed to prevent R's automatic coercion rules from changing the input. Be sure to test output before use.

group_dfs are ungrouped and nest_dfs are unnested.

Examples

#----------------------------------------------------------------------------
# as_df() examples
#----------------------------------------------------------------------------
library(bkdat)

m <- matrix(c(1, 2, 3, 4, 5, 6), ncol = 2)
as_df(m)
#>   V1 V2
#> 1  1  4
#> 2  2  5
#> 3  3  6
as.data.frame(m)
#>   V1 V2
#> 1  1  4
#> 2  2  5
#> 3  3  6

l <- list(a = c(1, 2, 3), b = c(4, 5, 6))
as_df(l)
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6
as.data.frame(l)
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6