[R] Converting characters back to Date and Time

Andrew Simmons @kw@|mmo @end|ng |rom gm@||@com
Tue Aug 31 23:23:08 CEST 2021


Hello,


I'm assuming you're reading from an "*.xlsx" file. I'm not sure which
package you're using for this scenario, but my preference is 'openxlsx'. If
this is the package you're using, you could set argument 'detectDates' to
'TRUE', and then it should read them correctly.


FILE <- tempfile(fileext = ".xlsx")


openxlsx::write.xlsx(
    data.frame(V1 = as.Date("2000-01-01")),
    FILE
)


openxlsx::read.xlsx(FILE)
openxlsx::read.xlsx(FILE, detectDates = TRUE)


unlink(FILE)


The first one should read the dates as numbers (days since 1900-01-01 or
1904-01-01, depending upon setup), while the second should parse them to
class "Date". I hope this helps!

On Tue, Aug 31, 2021 at 4:26 PM Eliza Botto <eliza_botto using outlook.com> wrote:

> DeaR useR,
>
> I read an excel column in R having Date and time (written in the same
> cell) as follow,
>
> 06/18/18 10:00
>
> 06/18/18 11:00
>
> 06/18/18 12:00
>
> In R environment, they are read as
>
> 43269.42
>
> 43269.46
>
> 43269.50
>
> Is there a way to covert these characters back to the original format?
>
> Thank-you very much in advance.
>
>
> Eliza Botto
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list