相关文章推荐
博学的绿茶  ·  使用Resource Hacker ...·  2 月前    · 
博学的绿茶  ·  Utility Types | Vue.js·  10 月前    · 
博学的绿茶  ·  Help And Training ...·  11 月前    · 
博学的绿茶  ·  r - Caused by error ...·  1 年前    · 
独立的眼镜  ·  如何连接Babelfish for RDS ...·  1小时前    · 
发财的蛋挞  ·  Microsoft Azure Data ...·  1小时前    · 
冷冷的投影仪  ·  Secure an ASP.NET ...·  1小时前    · 
不羁的生姜  ·  PSPSDK 开发的时候出现 ...·  1小时前    · 
儒雅的投影仪  ·  Perl 包和模块 | ·  3 小时前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Caused by error in `as.POSIXlt.character()`: ! character string is not in a standard unambiguous format. How can I change CHR to POSIXCT?

Ask Question

The most important columns of dates have chr class and I need it to be converted to POSIXct for calculations.

combined_ctd <-
  mutate(cyclist_trip_data_all, ride_length =
           difftime(ended_at,started_at,units='mins'))

The above throws the unambiguous format error at us and I'm pretty sure because the two columns are chr class.

cyclist_trip_data_all %>%
    transmute(ended_at = as.POSIXct(ended_at,tz="",tryFormats=
                                    c("%Y-%m-%d %H:%M:%OS",
                                      "%Y/%m/%d %H:%M:%OS",
                                      "%Y-%m-%d %H:%M",
                                      "%Y/%m/%d %H:%M",
                                      "%Y-%m-%d",
                                      "%Y/%m/%d")))
head(cyclist_trip_data_all$ended_at)

Using the above code throws similar unambiguous format error:

Error in `transmute()`:
! Problem while computing `ended_at = as.POSIXct(...)`.
Caused by error in `as.POSIXlt.character()`:
! character string is not in a standard unambiguous format
Traceback:
1. cyclist_trip_data_all %>% transmute(ended_at = as.POSIXct(ended_at, 
 .     tz = "", tryFormats = c("%Y-%m-%d %H:%M:%OS", "%Y/%m/%d %H:%M:%OS", 
 .         "%Y-%m-%d %H:%M", "%Y/%m/%d %H:%M", "%Y-%m-%d", "%Y/%m/%d")))
2. transmute(., ended_at = as.POSIXct(ended_at, tz = "", tryFormats = c("%Y-%m-%d %H:%M:%OS", 
 .     "%Y/%m/%d %H:%M:%OS", "%Y-%m-%d %H:%M", "%Y/%m/%d %H:%M", 
 .     "%Y-%m-%d", "%Y/%m/%d")))
3. transmute.data.frame(., ended_at = as.POSIXct(ended_at, tz = "", 
 .     tryFormats = c("%Y-%m-%d %H:%M:%OS", "%Y/%m/%d %H:%M:%OS", 
 .         "%Y-%m-%d %H:%M", "%Y/%m/%d %H:%M", "%Y-%m-%d", "%Y/%m/%d")))
4. mutate_cols(.data, dots, caller_env = caller_env())
5. withCallingHandlers({
 .     for (i in seq_along(dots)) {

I've tried as.POSIXct(as.numeric(as.character())) function instead but it changes every date values in the column into NA introduced by coersion.

Also tried:

cyclist_trip_data_all$ended_at <- as.POSIXct(cyclist_trip_data_all$ended_at,format="%Y-%m-%d %H:%M:%S",tz="UTC")
cyclist_trip_data_all$started_at <- as.POSIXct(cyclist_trip_data_all$ended_at,format="%Y-%m-%d %H:%M:%S",tz="UTC")

And it also changes every value to NA.

How do I actually change it to a POSIXct class without it becoming NA?

To note, this error doesn't happen in RStudio but it's happening in my Kaggle notebook for R.

There are alternatives -- at some point I found having to chase (known) formats to be too repetitive and boring and wrote a package that does it for me (and fast):

> library(anytime)
> datevec <- c("5/30/2021 11:58", "5/30/2021 12:10", "5/30/2021 11:29")
> anytime(datevec)
[1] "2021-05-30 11:58:00 CDT" "2021-05-30 12:10:00 CDT" "2021-05-30 11:29:00 CDT"

The example just shows the first three of your (non-reproducibly presented) dates. The package has other functions too for converting dates, or specific timezones as well as formatters. Take a look: anytime at CRAN -- and yes it of course also works in pipes and with other packages and whatnot. It "just" aims to take care of converting 'any time or date in any format' to POSIXct or Date.

I see great and helpful people answering here all the time but I have no idea what reproducible means when they ask for it along with dput? Can you help me ask a question in a better and cleaner? – LordPuggo May 14, 2022 at 4:50 @LordPuggo This post may help you create a minimal reproducible example. If it doesn't there are many, many others that attempt to do the same. The guiding principle is give us enough information to allow us to reproduce your problem easily. Just that, and no more. – Limey May 14, 2022 at 8:53 While finding the right format is boring, it's still my prefered way. Doing this automatically is error-prone, especially if the function is not able to print a warning in ambiguous cases. And after having been bitten many times by automatic guesses by R (including the coltypes on import), I have found that it's better to never ever rely on this. It's not surprising that anytime(c("10/05/2020")) yields a date in october, while in France it would usually be may. – user13963867 May 14, 2022 at 15:57 It's indeed a tradeoff but there are users in North America and Europe so we see mon-day and day-mon. Splitting by common separator helps somewhat but I also strongly recommend (and use) ISO 8601 whereever I can. In any event, this package is widely used and appreciated by many -- and we all have choices, including the one you exercise to ignore it. But trust when I say it was seen as helpful by many here when they struggled with formats. It also removes the tedium from having to add an origin etc. Cheers. – Dirk Eddelbuettel May 14, 2022 at 16:04

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章