spec_update: Update a readr column specification.

Description Usage Arguments Details Value Examples

Description

Given a cols specification, update the specification in place by explicitly declaring the appropriate type in a dataframe.

Usage

1
spec_update(col_spec, col_spec_df)

Arguments

col_spec

A cols specification. Most commonly produced as part of a call to read_csv.

col_spec_df

A dataframe containing the column name and type to update within the specification. The expectation is that col_spec_df has columns named col_name and col_type. If col_spec_df has columns named differently, this is easy to convert using rename.

Details

This is of value when importing an extremely wide dataframe with many columns. It may not make sense to explicitly declare the type of every column by hand. Nor should one have to copy/paste an exisiting column specification into a text editor in order to update the specification.

A final use case is when problems occur and a problems tibble is created. The tibble can be the starting point to decide upon the columns that require updating.

spec_update is a closure that contains two parts.

Value

A copy of the original specification with updated column types.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
test_df <- readr::read_csv("a,b,c\n1,2,3\n4,5,6")
test_spec <- readr::spec(test_df)

## returns
##   a = col_integer()
##   b = col_integer()
##   c = col_integer()

## update columns a and b to be doubles instead of integers
col_spec_df <- tibble::tribble(
  ~col_name, ~col_type,
  "a", "double",
  "b", "double"
)

## update the specification
test_spec_updated <- spec_update(test_spec, col_spec_df)

## re-read with new column spec
test_updated <- readr::read_csv("a,b,c\n1,2,3\n4,5,6",
                                col_types = test_spec_updated)

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