I have data imported from a CSV in R. The DateTime is in the excel format (i.e., 44410.095193, 44410.095203, etc).
Here is the dupt from a small portion of my data:
> dput(tail(raw.data[,1]))
structure(list(DateTime = c(44410.095193, 44410.095203, 44410.095215,
44410.095227, 44410.095238, NA)), row.names = c(NA, -6L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x000002048fb61ef0>)
I've found that there are a couple of easy ways to convert this to a different format (i.e., 2021-08-02 02:17:04, 2021-08-02 02:17:05, etc) by using XLDateToPOSIXct
from the DescTools
package and convertToDateTime
form the openxlsx
package on the desktop version of R.
However, these functions don't work in the Jupyter Notebook version of R. I've tried to install the packages listed above in the Jupyter Notebook, but afterwards I always get an error saying:
#for the openxlsx package
Error in convertToDateTime(raw.data$DateTime): could not find function "convertToDateTime"
Traceback:
#for the DescTools package
Error in XLDateToPOSIXct(raw.data$DateTime): could not find function "XLDateToPOSIXct"
Traceback:
I am thinking there must be another way to do the conversions. I've tried the as.Date
and as.POSIXct
functions, but these aren't giving me all the data I need (just the date and not the time) or is incorrect (all years are 1899).
What are some functions that would work in Jupyter Notebook that would convert the numeric version of a DateTime to a an actual date/time (e.g., 2021-08-02 02:17:05)?
CodePudding user response:
A base R solution that should work in your notebook. The second as.POSIXct
enables the time part. Make sure that your data.frame dates are numeric
.
dat <- c(44410.095193, 44410.095203, 44410.095215, 44410.095227, 44410.095238, NA)
class(dat)
[1] "numeric"
as.POSIXct( as.Date( dat, origin="1899-12-30" ) )
[1] "2021-08-02 04:17:04 CEST" "2021-08-02 04:17:05 CEST"
[3] "2021-08-02 04:17:06 CEST" "2021-08-02 04:17:07 CEST"
[5] "2021-08-02 04:17:08 CEST" NA