15.1 Creating a Data Frame

15.1.1 Problem

You want to create a data frame from vectors.

15.1.2 Solution

You can put vectors together in a data frame with data.frame():

15.1.3 Discussion

A data frame is essentially a list of vectors and factors. Each vector or factor can be thought of as a column in the data frame.

If your vectors are in a list, you can convert the list to a data frame with the as.data.frame() function:

The tidyverse way of creating a data frame is to use data_frame() or as_data_frame() (note the underscores instead of periods). This returns a special kind of data frame – a tibble – which behaves like a regular data frame in most contexts, but prints out more nicely and is specifically designed to play well with the tidyverse functions.

A regular data frame can be converted to a tibble using as_tibble():