0

I'm looking to write a script that can simply correct dates in a data frame (observations) that have two digit years to have four digits. I have all of the logic, but I get an error when I run this code:

observations[1,"Datetime_UTC"] <- observations[1,"Datetime_UTC"] + years(2000)

This line results in:

# Error in as.POSIXlt.numeric(value) : 'origin' must be supplied

How do I resolve this error?

Thank you!

reisnern21
  • 107
  • 2
  • 9
  • 1
    That error indicates that the value you are converting to `POSIXlt` is just a number, not a string or existing `POSIXt`-friendly format. Because of that, how to interpret it depends on the source of the data. Many tools use the unix "epoch" (seconds since "1970-01-01 00:00:00 UTC"), but many (including Excel) use different origins and units. Try adding `origin="1970-01-01"` (if epoch seconds) – r2evans Sep 15 '20 at 20:12
  • 1
    Also: You should NOT use POSIXlt vectors in data.frames. Use POSIXct instead. – IRTFM Sep 15 '20 at 20:28
  • @IRTFM Why should POSIXlt vectors not be used in data.frames? – reisnern21 Sep 15 '20 at 20:39
  • Because POSIXlt vectors are actually lists and dataframe functions do not support accessing dataframe columns that are lists. https://stackoverflow.com/questions/43799852/add-posixlt-as-a-new-column-to-a-dataframe – IRTFM Sep 15 '20 at 20:58

1 Answers1

2

If your dates are stored with some regular formatting, but with 2-digit years, just let lubridate functions handle it. They can handle most delimiters (even unusual ones).

library(lubridate)
ymd("20/11/9")
"2020-11-09"
dmy("31-1-90")
"1990-01-31"
mdy("3_07_00")
"2000-03-07"
Ben Norris
  • 5,639
  • 2
  • 6
  • 15
  • Thank you for the help! I had tried those before and they hadn't worked, but I hadn't tried mdy_hms(), which is how my data are formatted. – reisnern21 Sep 15 '20 at 20:29