Problems with using as.POSIXct, it worked once and never again

I've been trying for awhile now to get my date-time column converted from character to POSIXct format using that function. For some reason I cannot figure out, it works with one set of data and does not work for any other set.

The example below is the one that works.

> head(Test104M)
        date_time time.lag eastingx northingy hdop
1 2021-3-21 11:58        0   457008   4553323  1.3
2 2021-3-21 12:00        2   456999   4553322  1.3
3  2021-3-22 0:00      720   454743   4556604  8.3
4  2021-3-22 2:00      120   454770   4557051  4.0
5  2021-3-22 4:00      120   454708   4556829 21.0
6  2021-3-22 6:00      120   454783   4556909  1.5

Test104M$date_time <- as.POSIXct(Test104M$date_time,
                                 format = "%Y-%m-%d %H:%M")
str(Test104M)
'data.frame':	970 obs. of  5 variables:
 $ date_time: POSIXct, format: "2021-03-21 11:58:00" "2021-03-21 12:00:00" "2021-03-22 00:00:00" "2021-03-22 02:00:00" ...
 $ time.lag : int  0 2 720 120 120 120 120 120 120 720 ...
 $ eastingx : int  457008 456999 454743 454770 454708 454783 454623 454768 454828 454763 ...
 $ northingy: int  4553323 4553322 4556604 4557051 4556829 4556909 4556621 4556719 4556764 4556708 ...
 $ hdop     : num  1.3 1.3 8.3 4 21 1.5 3 6.4 5.1 4.4 ...

And then this one is the one that doesn't for some reason.

> head(Test104Ma)
        date_time time.lag eastingx northingy hdop
1 2021/3/21 11:58        0   457008   4553323  1.3
2 2021/3/21 12:00        2   456999   4553322  1.3
3  2021/3/22 0:00      720   454743   4556604  8.3
4  2021/3/22 2:00      120   454770   4557051  4.0
5  2021/3/22 4:00      120   454708   4556829 21.0
6  2021/3/22 6:00      120   454783   4556909  1.5

Test104Ma$date_time <- as.POSIXct(Test104Ma$date_time,
                                 format = "%Y-%m-%d %H:%M")
str(Test104Ma)
'data.frame':	970 obs. of  5 variables:
 $ date_time: POSIXct, format: NA NA NA NA ...
 $ time.lag : int  0 2 720 120 120 120 120 120 120 720 ...
 $ eastingx : int  457008 456999 454743 454770 454708 454783 454623 454768 454828 454763 ...
 $ northingy: int  4553323 4553322 4556604 4557051 4556829 4556909 4556621 4556719 4556764 4556708 ...
 $ hdop     : num  1.3 1.3 8.3 4 21 1.5 3 6.4 5.1 4.4 ...

I copy-pasted all the data from the original .csv (Test104M) into a new .csv (Test104Ma) to keep the variables equal and it obviously didn't work for the copy.

If anyone can tell me what I'm doing wrong to replicate the successful as.POSIXct I'd greatly appreciate it. This is my first time posting to the forum so if I can make my formatting better or offer more info let me know.

In your second example, the separator within the date part is a /, not a -. Here is an example of what you are doing and of what will work.

as.POSIXct("2021/3/21 11:58", format = "%Y-%m-%d %H:%M")
[1] NA
as.POSIXct("2021/3/21 11:58", format = "%Y/%m/%d %H:%M")
[1] "2021-03-21 11:58:00 EDT"

By the way, the functions is the lubridate_ package can help you avoid these sorts of problems as they take care of detecting the separators.

Phew thank you!! It's always those small things huh :sweat_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.