user1177819
user1177819

Reputation: 113

Dates in R differ between Windows and Linux

I have a data frame in R names as data.

data <- as.xts(read.zoo("data1.csv",sep=",",tz="" ,header=T))

data index in the format 2004-01-04 09:44:00 IST

I applied the operation to change the index to Dates

index(data) <- as.Date(index(data))

Output should be 2004-01-04 but system produces 2004-01-03.

This works correctly in Windows but does not work on Linux.

Upvotes: 1

Views: 321

Answers (2)

Richie Cotton
Richie Cotton

Reputation: 121127

Arun is correct that the problem is with locale. Your data has an Indian Standard Time stamp, and you have a US locale, which is at least 10.5 hours behind. Hence the time 09:44 is actually late the previous evening in your time zone.

Dates and time are horribly complicated, and R uses underlying OS capabilities to make it's calculations, which is why you see different results on different machines. Linux is POSIX compliant and understands time zones like "IST", which allows it to make the change to the previous night. Windows does not, which is why it gives the date as 01-04. To get the correct time zone update on Windows, you need to specify the full name of the time zone, "Asia/Kolkata". Wikipedia has a list of time zone names.


EDIT: Actually, R ships with a file containing all the "Continent/City" (Olson-style) names that it accepts. It is stored in

file.path(R.home("share"), "zoneinfo", "zone.tab")

and the example on the help page ?Sys.timezone tells you how to programmatically read it.


I find the lubridate package makes things a little easier to see what is happening.

library(lubridate)
x <- ymd_hms("2004-01-04 09:44:00 IST", tz = "Asia/Kolkata")
x
# [1] "2004-01-04 09:44:00 IST"
with_tz(x, "America/New_York")
# [1] "2004-01-03 23:14:00 EST"

Upvotes: 3

Arun
Arun

Reputation: 118819

Date <- c("2010-01-04 09:04:00", "2010-01-04 09:05:00")
Open <- c(5222.9, 5220.2)
Low <- c(5224.6, 5222.95)
High <- c(5220.1, 5218.6)
Close <- c(5220.35, 5222.95)
x <- data.frame(Date = Date, Open = Open, Low = Low, High = High, Close = Close)
as.Date(x$Date)

Output:

[1] "2010-01-04" "2010-01-04"

It seems alright to me.

Edit:

require(zoo)
data <- as.xts(read.zoo("data1.csv",sep=",",tz="" ,header=T))
> dput(data)

structure(c(5222.9, 5220.2, 5224.6, 5222.95, 5220.1, 5218.6, 
5220.35, 5222.95), .Dim = c(2L, 4L), .Dimnames = list(NULL, c("Open", 
"Low", "High", "Close")), index = structure(c(1262592240, 1262592300
), tzone = "", tclass = c("POSIXct", "POSIXt")), class = c("xts", 
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", 
"POSIXt"), .indexTZ = "", tzone = "")

> as.Date(index(data))

[1] "2010-01-04" "2010-01-04"

On my Mac it works right. I suspect your system locale is set wrong. Also, you may want to check it within R.

What does this command Sys.getlocale() give you in Windows and in Linux within R?

Upvotes: 1

Related Questions