Skip to contents

Handy vector construction from string

Usage

as_vec(x, cast_num = TRUE)

Arguments

x

String. A string to construct a vector from (see examples). If x has length > 1, stom::as_c_num or stom::as_c_chr would be called (depending on input type) to print out the concatenation syntax c(...) in the console.

cast_num

Boolean. Whether to convert string to numeric when applicable. See examples.

Examples

# Construct vector from string
as_vec("a b c")
#> [1] "a" "b" "c"
as_vec("d, e  f")   # Commas have precedence over spaces
#> [1] "d"    "e  f"

# Auto typecasting
as_vec("1 2 -3.1")
#> [1]  1.0  2.0 -3.1
as_vec("04, 05")
#> [1] 4 5
as_vec("04, 05", cast_num=FALSE)  # Disable typecasting
#> [1] "04" "05"