Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

vector-construction

Create vectors


Description

Questioning lifecycle

The atomic vector constructors are equivalent to c() but:

  • They allow you to be more explicit about the output type. Implicit coercions (e.g. from integer to logical) follow the rules described in vector-coercion.

  • They use dynamic dots.

Usage

lgl(...)

int(...)

dbl(...)

cpl(...)

chr(...)

bytes(...)

Arguments

...

Components of the new vector. Bare lists and explicitly spliced lists are spliced.

Life cycle

  • All the abbreviated constructors such as lgl() will probably be moved to the vctrs package at some point. This is why they are marked as questioning.

  • Automatic splicing is soft-deprecated and will trigger a warning in a future version. Please splice explicitly with !!!.

Examples

# These constructors are like a typed version of c():
c(TRUE, FALSE)
lgl(TRUE, FALSE)

# They follow a restricted set of coercion rules:
int(TRUE, FALSE, 20)

# Lists can be spliced:
dbl(10, !!! list(1, 2L), TRUE)


# They splice names a bit differently than c(). The latter
# automatically composes inner and outer names:
c(a = c(A = 10), b = c(B = 20, C = 30))

# On the other hand, rlang's ctors use the inner names and issue a
# warning to inform the user that the outer names are ignored:
dbl(a = c(A = 10), b = c(B = 20, C = 30))
dbl(a = c(1, 2))

# As an exception, it is allowed to provide an outer name when the
# inner vector is an unnamed scalar atomic:
dbl(a = 1)

# Spliced lists behave the same way:
dbl(!!! list(a = 1))
dbl(!!! list(a = c(A = 1)))

# bytes() accepts integerish inputs
bytes(1:10)
bytes(0x01, 0xff, c(0x03, 0x05), list(10, 20, 30L))

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.