0

I'm having trouble using a function POSIXct. When I apply the function in my dataset, the year appears with two zeros ahead. like this:


datu1$timestamp <- as.POSIXct(datu1$date.sec, origin = "1970-01-01", tz="GMT")
datu1$timestamp <- as.POSIXct(datu1$timestamp, 
                                     format = "%Y-%m-%d %H:%M:%S", tz = 'GMT')

head(datu1)

  ID     date.sec      lon       lat  lon.025   lat.025 lon.5  lat.5  lon.975   lat.975
1 102211.10 -61827840000 -38.6616 -13.59272 -40.5025 -15.25025 -38.7 -13.76 -36.9000 -10.88950
2 102211.10 -61827818400 -38.6647 -13.60312 -40.4000 -15.17025 -38.7 -13.77 -37.0975 -11.03975
3 102211.10 -61827796800 -38.6723 -13.64505 -40.3000 -15.10000 -38.7 -13.79 -37.0000 -11.29950
4 102211.10 -61827775200 -38.6837 -13.68972 -40.2000 -14.98025 -38.7 -13.83 -37.2000 -11.45975
5 102211.10 -61827753600 -38.7030 -13.73054 -40.2000 -14.98100 -38.7 -13.84 -37.3000 -11.62925
6 102211.10 -61827732000 -38.7221 -13.77846 -40.0000 -15.04050 -38.7 -13.88 -37.5000 -11.69950
  bmode bmode.5           timestamp
1 1.556       2 0010-10-03 00:00:00
2 1.565       2 0010-10-03 06:00:00
3 1.571       2 0010-10-03 12:00:00
4 1.571       2 0010-10-03 18:00:00
5 1.589       2 0010-10-04 00:00:00
6 1.599       2 0010-10-04 06:00:00

How can I fix this to get the full year (like: 2010) instead of two zeros?

Anne Elise
  • 133
  • 2
  • 9
  • 3
    You need to start from proper data. `as.POSIXct(-61827840000, origin="1970-01-01")` gets the year 10. So check your input data. – Dirk Eddelbuettel Jun 10 '20 at 15:52
  • 1
    Said another way (not that Dirk needs correction): garbage-in, garbage-out. The code you are using is correct, the data is "patently" questionable in this case. Is this the raw data as you imported it? Or have you done some transformation before this that might have mis-calculated something? – r2evans Jun 10 '20 at 16:00
  • @r2evans The raw data was the same data frame without the "timestamp" column. Why is the data "patently" questionable? Can you explain more? – Anne Elise Jun 10 '20 at 16:17
  • @r2evans I went to check the dataframe and some dates are correct, others are not. is it possible to correct the data manually? The dates are correct, but in some cases this error appeared, precisely because of the input data. – Anne Elise Jun 10 '20 at 16:26
  • Do you have data from the year 10? If it were the year 2010, the `date.sec` would be around `as.numeric(as.POSIXct("2010-01-01"))`, or 1,262,332,800. If it were the year 1910, the `date.sec` would be around `as.numeric(as.POSIXct("1910-01-01"))`, or -1,893,427,200 (negative!). But your `date.sec` is around -61,827,840,000. The fact that you object to the outcome of `0010` as the year suggests that you believe it to be more modern (1910 or 2010), so to me having epoch seconds on the order of -61bn is questionable. Perhaps my use of *"patently"* was too strong, but ... perhaps not. – r2evans Jun 10 '20 at 16:26
  • *"correct the data manually"*, perhaps, but (1) I can't do it, I know nothing about the source of the data; because (2) any munging of the data is changing data, making the results somewhat questionable. If this is a "funded controlled study", then this right here would be grounds to invalidate all of this data (until the truth is determined without "fixing the data manually"). – r2evans Jun 10 '20 at 16:29

1 Answers1

0

Perhaps your data was encoded with a weird origin (e.g. excel uses "1899-12-30"). Just adapt the origin= 'till the date matches what you require.

as.POSIXct(-61827840000, origin="1970-01-01", tz="GMT")
# [1] "0010-10-03 GMT"
as.POSIXct(-61827840000, origin="3970-01-01", tz="GMT")
# [1] "2010-10-03 GMT"
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Funnily plausible, jay.sf ... do you know of any systems that default to an origin in the year 3970? ;-) (Jokes aside, I suggest that *"adapt the `origin=`"* is akin to p-value seeking ... meaning not good.) – r2evans Jun 10 '20 at 16:31
  • 1
    @r2evans Nope, must be somewhat alien :) Anyway, I understood OP wanted `"2010"` instead of `"0010"` - that's a way how to do it. – jay.sf Jun 10 '20 at 16:34