Import with defined type #387
JanMarvin
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Extended answer presented previously on StackOverflow.
Sometimes cell types on spreadsheets can be messy. In the example below we want to import a column which contains dates as characters and numerics. In spreadsheet software they might look the same. If we want to import this column in
openxlsx2
we can define a specific column type (currently typescharacter
,numeric
andDate
are supported).the example workbook
This creates a file that causes the behavior seen in the StackOverflow question observed with
openxlsx
. Some of the rows in theRegistrationdate
column are formatted as dates and some as strings, a fairly common error caused by the someone who generated the xlsx input and often hard to spot in spreadsheet software.what is imported
As we can see below,
openxlsx
does not convert theDate
variables if a column consists of a mix ofDate
and character entries. Inopenxlsx2
we do the date conversion earlier, therefore we already see dates, but in our outputRegistrationdate
is still a character.With
openxlsx2
you can define the type of column you want to import. The option was inspired by readxl (iirc).import with requested type
A solution to import the column as date is the
type
argument forwb_to_df()
. If we select to import the column asDate
it will be converted toDate
no matter what.Beta Was this translation helpful? Give feedback.
All reactions