as_vector: Coerce a list to a vector

Description Usage Arguments Details Examples

View source: R/coercion.R

Description

as_vector() collapses a list of vectors into one vector. It checks that the type of each vector is consistent with .type. If the list can not be simplified, it throws an error. simplify will simplify a vector if possible; simplify_all will apply simplify to every element of a list.

Usage

1
2
3
4
5

Arguments

.x

A list of vectors

.type

A vector mold or a string describing the type of the input vectors. The latter can be any of the types returned by typeof(), or "numeric" as a shorthand for either "double" or "integer".

Details

.type can be a vector mold specifying both the type and the length of the vectors to be concatenated, such as numeric(1) or integer(4). Alternatively, it can be a string describing the type, one of: "logical", "integer", "double", "complex", "character" or "raw".

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Supply the type either with a string:
as.list(letters) %>% as_vector("character")

# Or with a vector mold:
as.list(letters) %>% as_vector(character(1))

# Vector molds are more flexible because they also specify the
# length of the concatenated vectors:
list(1:2, 3:4, 5:6) %>% as_vector(integer(2))

# Note that unlike vapply(), as_vector() never adds dimension
# attributes. So when you specify a vector mold of size > 1, you
# always get a vector and not a matrix

Example output

 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
[1] 1 2 3 4 5 6

purrr documentation built on Aug. 3, 2017, 1:03 a.m.