R/type_ensure.R
type_ensure.Rd
Ensure the specific columns of a dataframe have a particular type.
type_ensure(df, ensure_nms, type = "numeric")
A dataframe.
Character vector giving names of df
to ensure type
A string giving the type to ensure in columns ensure_nms
A modified version of df
, with columns (specified in ensure_nms
)
of type type
.
Other functions to operate on column types:
type_vft()
Other functions for developers:
check_crucial_names()
,
extract_insensitive()
,
flag_if_group()
,
is_multiple()
,
nms_try_rename()
,
rename_matches()
dfm <- tibble(
w = c(NA, 1, 2),
x = 1:3,
y = as.character(1:3),
z = letters[1:3]
)
dfm
#> # A tibble: 3 × 4
#> w x y z
#> <dbl> <int> <chr> <chr>
#> 1 NA 1 1 a
#> 2 1 2 2 b
#> 3 2 3 3 c
type_ensure(dfm, c("w", "x", "y"), "numeric")
#> Warning: y should be numeric. Type found: character
#> * Changing type (of y) accordingly.
#> # A tibble: 3 × 4
#> w x y z
#> <dbl> <int> <dbl> <chr>
#> 1 NA 1 1 a
#> 2 1 2 2 b
#> 3 2 3 3 c
type_ensure(dfm, c("w", "x", "y", "z"), "character")
#> Warning: w, x should be character. Type found: double, integer
#> * Changing type (of w, x) accordingly.
#> # A tibble: 3 × 4
#> w x y z
#> <chr> <chr> <chr> <chr>
#> 1 NA 1 1 a
#> 2 1 2 2 b
#> 3 2 3 3 c