I want to use a metadata file to read in column formats to use in readr. And some of the formats I need to specify are custom date formats. So instead of hardcoding the column_formats like this...
readr::read_csv(readr_example('mtcars.csv'),
col_types = list(cyl=col_integer(), carb=col_integer()))
...I want to read the formats from a csv file, for example
metadata <- readr::read_csv("Field,Format
cyl,col_integer()
carb,col_integer()"
)
mycoltypes <- setNames(as.list(metadata$Format),metadata$Field)
readr::read_csv(readr_example('mtcars.csv'), col_types =mycoltypes)
This doesn't work because my named list is a list of character strings rather than column objects.
I know there is the option of using the abbreviated character names, so I can use "i" instead of "col_integer" in my csv file and that will work. But as I said, I also want to specify date formats.
With abbreviated names, using "D"
, it is not possible to specify custom date format, like col_date(format="%Y%m%d")
.
Any ideas how I can include this custom date format in my metadata file and then turn it into a named list of column objects?
I have no control over the date format in my input data set.
Thanks