as_data_frame {dplyr} | R Documentation

## Coerce a list to a data frame.

### 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 `cbind`ing together). `as_data_frame` just verifies that the list is structured correctly (i.e. named, and each element is same length) then sets class and row name attributes.

### Usage

    as_data_frame(x)

### Arguments

`x` | A list. Each element of the list must have the same length.

### Examples

    l <- list(x = 1:500, y = runif(500), z = 500:1)
    df <- as_data_frame(l)

    # Coercing to a data frame does not copy columns
    changes(as_data_frame(l), as_data_frame(l))

    # as_data_frame is considerably simpler/faster than as.data.frame
    # making it more suitable for use when you have things that are
    # lists
    ## Not run:
    l2 <- replicate(26, sample(letters), simplify = FALSE)
    names(l2) <- letters
    microbenchmark::microbenchmark(
      as_data_frame(l2),
      as.data.frame(l2)
    )

    ## End(Not run)

[Package dplyr version 0.4.3 Index]