Description Usage Arguments Details Value Examples
Given a cols specification, update the specification in place by explicitly declaring the appropriate type in a dataframe.
1 | spec_update(col_spec, col_spec_df)
|
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 |
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.
An inner function, update_
, that updates the specification by
setting the class
attribute for the given column.
The enclosing environment that contains the specification to be updated and contains a call to pmap to drive iteration.
A copy of the original specification with updated column types.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.