7

When I try to execute the following code on RStudio

library(lubridate)
library(data.table)

a <- data.frame(c("2017-12-01 00:01:00","2017-12-02 00:01:00"),c(5,6))
colnames(a) <- c("t", "x")
a <- as.data.table(a)
a[, t := parse_date_time(t, orders = "ymd HMS")]
print(class(a$t))
paste("a:", format(a[1,1], format = "%Y-%m-%d %H:%M:%S"))

I have the following result:

[1] "a: 2017-12-01 00:01:00"

But when I try to run the same code under Rscript, the output is

[1] "a: 1"

Does anyone have the same problem as me? Any help is appreciated.

EDIT: R version 3.3.1, platform x86_64-w64-mingw32/x64.

Windows server 2012 version:

Major Minor Build Revision

6 2 9200 0

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • 2
    For me both gave same output. – sm925 Dec 22 '17 at 19:20
  • clean/clear your environment if you can and rerun the chunk again – Onyambu Dec 22 '17 at 19:21
  • I already did it, the problem continues. I think I'm going to add more details, such as running it from windows server – Lucio Carlos Pimentel Paiva Dec 22 '17 at 19:24
  • 2
    Could you run Sys.getlocale() in both environments and paste the output here? – Borislav Aymaliev Dec 28 '17 at 13:09
  • In R studio: "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252" Rscript output in Powershell: "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC _NUMERIC=C;LC_TIME=English_United States.1252" – Lucio Carlos Pimentel Paiva Dec 28 '17 at 18:17
  • Try adding `print(attributes(a$t))`. You may need to specify a timezone and/or Locale in your `parse_date_time` call. – zlipp Dec 28 '17 at 23:27
  • When I add `print(attributes(a$t))` I get $tzone [1] "UTC" $class [1] "POSIXct" "POSIXt" both in R Studio and Rscript. Specifying a timezone doesn't solve the issue. – Lucio Carlos Pimentel Paiva Dec 29 '17 at 11:42
  • 3
    For me, this script makes Rscript auto-load `methods` (with a message). In interactive R, `methods` is one of the always-loaded core packages, but historically it hasn't been loaded by Rscript, so I suspect the auto-loading is a recent-ish change. Try updating R and/or explicitly adding `library(methods)` to your script. – alistaire Dec 31 '17 at 16:01
  • I tried explicitly calling `library(methods)` and the problem persists. Unfortunately, I can't update R because it may break production code. I got around this bug calling `a[1,t]` instead of `a[1,1]`, which gives me the correct output in both Rstudio and Rscript (so in practical terms the problem is solved). Thanks anyway. – Lucio Carlos Pimentel Paiva Jan 02 '18 at 10:54
  • @LucioCarlosPimentelPaiva can you launch a command window on Windows and show the output of `systeminfo | findstr /C:"Zone"` please. – Technophobe01 Jan 02 '18 at 20:57
  • Time Zone: (UTC-03:00) Brasilia – Lucio Carlos Pimentel Paiva Jan 03 '18 at 11:17

1 Answers1

2

The real problem is with data.table. In earlier versions of data.table, if we use numeric index, without with=FALSE, then it will return the numeric index itself. Hence the output, shared in question. For details on this, one can go through the documentation of data.table.

In recent versions of data.table, however, this issue has been resolved, and as a result, data.table pretty much works like data.frame. This is the reason, why no one was able to replicate the output shared in the question.

Below is the updated code, using with=FALSE option with data.table.

library(lubridate)
library(data.table)

a <- data.frame(c("2017-12-01 00:01:00","2017-12-02 00:01:00"),c(5,6), stringsAsFactors=FALSE) # stringsAsFactors=TRUE
colnames(a) <- c("t", "x")
a$t1 <- parse_date_time(a$t, orders = "ymd HMS")
a <- as.data.table(a)
a[, t := parse_date_time(as.character(t), orders = "ymd HMS")]
print(class(a$t))
paste("a:", format(a[1,1, with=FALSE], format = "%Y-%m-%d %H:%M:%S"))

You can get info on package version using using sessionInfo(). Using the package versions, the issue can be identified.

Kumar Manglam
  • 2,780
  • 1
  • 19
  • 28