首页 > 解决方案 > Datetime problem when using Lubridate function

问题描述

Code to reconstruct the df:

rdf=structure(list(caseid = c("60",
    "76", "33", "76", "33", "12", "35",
    "88", "32", "44"), indate = c("2020-05-05 17:41:28.277",
    "2020-08-31 03:29:31.110", "2020-10-13 09:34:00.887", "2020-08-21 11:51:18.157",
    "2020-10-13 09:34:12.173", "2020-08-06 11:55:30.857", "2019-12-17 15:02:05.747",
    "2019-11-22 10:12:29.247", "2020-05-16 01:27:17.960", "2020-07-04 19:47:50.587"
    ),countryid = c("TAIWAN",
    "INDIA", "SINGAPORE", "TAIWAN", "SINGAPORE", "SINGAPORE", "HONG KONG",
    "SINGAPORE", "SINGAPORE", "INDIA")), .Names = c("caseid", "indate", "countryid"), row.names = c(NA, 10L
    ), class = "data.frame")

rdf$indate = as.factor(rdf$indate)

Code to add 6 hours to indate when country is equal to Hong Kong and Macau.

library(dplyr)
library(lubridate)

rdf <- rdf %>%
mutate(indate = ymd_hms(indate)
,indate1 = if_else(countryid %in% c('HONG KONG','MACAU'), indate + hours(6), indate) )

rdf$indate = NULL
colnames(rdf)[6]='indate'
rdf = rdf[c("casenumber","caseid","indate","actiondate","countryid","caseflowid")]

 

When using this dataframe for a new function getting the below error:

Error in charToDate(x) :
    character string is not in a standard unambiguous format

标签: rdatetimelubridate

解决方案


推荐阅读