convert_cols: Convert dataframe column types.

Description Usage Arguments Details Value Examples

Description

Given a configuration dataframe with column names and the required types, convert the column data types in a separate dataframe.

Usage

1

Arguments

df

A dataframe that will have its column data types converted.

types_df

A dataframe containing the column to convert and the data type to convert to. The expectation is that types_df has columns named col_name and col_type. If types_df has columns named differently, this is easy to convert using rename.

Details

Column metadata may be stored separately from the code needed to perform the conversion. As an example, a dataframe with 2,000 columns could have its metadata stored in a separate csv or xlsx file. The metadata file would be read in as a dataframe and convert_cols would update the data types en masse.

convert_cols is trading the need to maintain a separate metadata file for concise code that performs the data type conversion. The alternative is to have verbose code that details each data type conversion to take place.

convert_cols is a closure that contains two parts.

Finally, the function is missing cases to convert to the following types. The types includes should cover the most simple use cases.

Value

A copy of the original dataframe, df, with the column types converted.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## dataframe to convert
id <- c(1L, 2L, 3L)
date_var <- c("2016-01-02", "2015-12-24", "2016-05-05")
datetime_var <- c("2016-01-02T11:11:04",
                  "2015-12-24T08:11:41",
                  "2016-05-05T05:05:49")
double_var1 <- c("46.41", "118.11", "84.68")
double_var2 <- c("68.48", "-248.99", "194")
numeric_var <- c("78.61", "593.1", "123")
logical_var <- c("true", "true", "false")
character_var <- c(49, 88, 104)
integer_var <- c("77", "84", "4949")

test_df <- data.frame(id,
                      date_var,
                      datetime_var,
                      double_var1,
                      double_var2,
                      numeric_var,
                      logical_var,
                      character_var,
                      integer_var)

## print the structure
str(test_df)

## create a dataframe with columns named col_name and col_type (if you name
##   them something else you can always use dplyr::rename) with the column
##   name and the type you would like it converted to
types_df <- tibble::tribble(
  ~col_name, ~col_type,
  "date_var", "date",
  "datetime_var", "datetime",
  "double_var1", "double",
  "double_var2", "double",
  "numeric_var", "numeric",
  "logical_var", "logical",
  "character_var", "character",
  "integer_var", "integer"
)

## convert the columns
test_df_converted <- convert_cols(test_df, types_df)

curtisalexander/CRAmisc documentation built on May 14, 2019, 12:52 p.m.