Skip to contents

Combine all arguments into a new vector of common type. This function is a renaming of vctrs::vec_c().

Usage

c2(..., .ptype = NULL, .name_spec = NULL, .name_repair = "minimal")

Arguments

...

Vectors to coerce.

.ptype

If NULL, the default, the output type is determined by computing the common type across all elements of ....

Alternatively, you can supply .ptype to give the output known type. If getOption("vctrs.no_guessing") is TRUE you must supply this value: this is a convenient way to make production code demand fixed types.

.name_spec

A name specification for combining inner and outer names. This is relevant for inputs passed with a name, when these inputs are themselves named, like outer = c(inner = 1), or when they have length greater than 1: outer = 1:2. By default, these cases trigger an error. You can resolve the error by providing a specification that describes how to combine the names or the indices of the inner vector with the name of the input. This specification can be:

  • A function of two arguments. The outer name is passed as a string to the first argument, and the inner names or positions are passed as second argument.

  • An anonymous function as a purrr-style formula.

  • A glue specification of the form "{outer}_{inner}".

  • "inner", in which case outer names are ignored, and inner names are used if they exist. Note that outer names may still be used to provide informative error messages.

  • An rlang::zap() object, in which case both outer and inner names are ignored and the result is unnamed.

See the name specification topic.

.name_repair

How to repair names, see repair options in vec_as_names().

Value

A vector with class given by .ptype, and length equal to the sum of the vec_size() of the contents of ....

The vector will have names if the individual components have names (inner names) or if the arguments are named (outer names). If both inner and outer names are present, an error is thrown unless a .name_spec is provided.

Examples

c2(FALSE, 1L, 1.5) #> c(0, 1.0, 1.5)
#> [1] 0.0 1.0 1.5

# Date/times:
c(Sys.Date(), Sys.time()) # Two Date-s
#> [1] "2026-09-17" "2026-09-17"
c(Sys.time(), Sys.Date()) # Two POSIXct-s
#> [1] "2026-09-17 11:41:09 -03" "2026-09-16 21:00:00 -03"
c2(Sys.Date(), Sys.time()) # Two POSIXct-s
#> [1] "2026-09-17 00:00:00 -03" "2026-09-17 11:41:09 -03"

# Factors:
c(factor("a"), factor("b")) # Factor with levels "a" and "b"
#> [1] a b
#> Levels: a b
c2(factor("a"), factor("b")) # Factor with levels "a" and "b"
#> [1] a b
#> Levels: a b

# By default, named inputs must be length 1:
c2(name = 1) #> c(name = 1)
#> name 
#>    1 
try(c2(name = 1:3)) #> Error
#> Error in vctrs::vec_c(..., .ptype = .ptype, .name_spec = .name_spec, .name_repair = "minimal") : 
#>   Can't merge the outer name `name` with a vector of length > 1.
#> Please supply a `.name_spec` specification.

# Pass a name specification to work around this:
c2(name = 1:3, .name_spec = "{outer}_{inner}")
#> name_1 name_2 name_3 
#>      1      2      3