df_typecast: Typecast columns of a data.frame

Description Usage Arguments Details Value Examples

View source: R/df_typecast.R

Description

Bulk-typecast columns of a data.frame. Use with care, will introduce NAs for impossible casts (like the base R as.* functions)

Usage

1
2
3
df_typecast(x, table_design, silent = FALSE)

df_typecast_list(x, conv, silent)

Arguments

x

a data.frame or list

table_design

a table_design

silent

logical. Should warnings be displayed?

conv

a list of the form list(colname = "coltype")

Details

df_typecast() uses a table_design Object to specify column types, while df_typecast_list() uses a list (see examples).

Value

Either a data.frame or list with typecasted columns/elements (depending on the type of x)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
dat <- data.frame(
  foo = c('5', '6', '5'),
  bar = factor(c('a', 'b', 'c')),
  stringsAsFactors = FALSE
)
str(dat)


td <- tabde(
  col_name = c("foo", "bar"),
  col_type = c("character", "factor")
)
res <- df_typecast(dat, td)
str(res)


res <- df_typecast_list(
  dat,
  list(foo = "character", bar = "factor")
)
str(res)

s-fleck/tabde documentation built on April 4, 2021, 10:11 p.m.