data_frame: Construct a data frame

Description Usage Arguments Details Properties See Also Examples

View source: R/type-data-frame.R

Description

data_frame() constructs a data frame. It is similar to base::data.frame(), but there are a few notable differences that make it more in line with vctrs principles. The Properties section outlines these.

Usage

1
2
3
4
5
data_frame(
  ...,
  .size = NULL,
  .name_repair = c("check_unique", "unique", "universal", "minimal")
)

Arguments

...

Vectors to become columns in the data frame. When inputs are named, those names are used for column names.

.size

The number of rows in the data frame. If NULL, this will be computed as the common size of the inputs.

.name_repair

One of "check_unique", "unique", "universal" or "minimal". See vec_as_names() for the meaning of these options.

Details

If no column names are supplied, "" will be used as a default for all columns. This is applied before name repair occurs, so the default name repair of "check_unique" will error if any unnamed inputs are supplied and "unique" will repair the empty string column names appropriately. If the column names don't matter, use a "minimal" name repair for convenience and performance.

Properties

See Also

df_list() for safely creating a data frame's underlying data structure from individual columns. new_data_frame() for constructing the actual data frame from that underlying data structure. Together, these can be useful for developers when creating new data frame subclasses supporting standard evaluation.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
data_frame(x = 1, y = 2)

# Inputs are recycled using tidyverse recycling rules
data_frame(x = 1, y = 1:3)

# Strings are never converted to factors
class(data_frame(x = "foo")$x)

# List columns can be easily created
df <- data_frame(x = list(1:2, 2, 3:4), y = 3:1)

# However, the base print method is suboptimal for displaying them,
# so it is recommended to convert them to tibble
if (rlang::is_installed("tibble")) {
  tibble::as_tibble(df)
}

# Named data frame inputs create data frame columns
df <- data_frame(x = data_frame(y = 1:2, z = "a"))

# The `x` column itself is another data frame
df$x

# Again, it is recommended to convert these to tibbles for a better
# print method
if (rlang::is_installed("tibble")) {
  tibble::as_tibble(df)
}

# Unnamed data frame input is automatically spliced
data_frame(x = 1, data_frame(y = 1:2, z = "a"))

Example output

  x y
1 1 2
  x y
1 1 1
2 1 2
3 1 3
[1] "character"
# A tibble: 3 x 2
  x             y
  <list>    <int>
1 <int [2]>     3
2 <dbl [1]>     2
3 <int [2]>     1
  y z
1 1 a
2 2 a
# A tibble: 2 x 1
    x$y $z   
  <int> <chr>
1     1 a    
2     2 a    
  x y z
1 1 1 a
2 1 2 a

vctrs documentation built on April 29, 2021, 5:08 p.m.