15.1 Creating a 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.
data_frame(g, x)
#> Warning: `data_frame()` was deprecated in tibble 1.1.0.
#> ℹ Please use `tibble()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: `...` must be empty in `format.tbl()`
#> Caused by error in `format_tbl()`:
#> ! `...` must be empty.
#> ✖ Problematic argument:
#> • options = options
#> # A tibble: 3 × 2
#> g x
#> <chr> <int>
#> 1 A 1
#> 2 B 2
#> 3 C 3
A regular data frame can be converted to a tibble using as_tibble()
: