1

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.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
user1177819
  • 103
  • 1
  • 3
  • 7
  • Date Open Low High Close 2010-01-04 09:04:00 5222.9 5224.6 5220.1 5220.35 2010-01-04 09:05:00 5220.2 5222.95 5218.6 5222.95 index(data) <- as.Date(index(data)) . If I type index(date)[1] on the command prompt it gives 2010-01-03 rather than 2010-01-04 – user1177819 Jan 04 '13 at 08:20
  • data.csv files contain data in the format Date,Open,Low,High,Close 2010-01-04 09:04:00,5222.9,5224.6,5220.1,5220.35 2010-01-04 09:05:00,5220.2,5222.95,5218.6,5222.95 2010-01-04 09:06:00,5222.25,5225,5222.25,5225 2010-01-04 09:07:00,5225,5225.9,5219.4,5219.4 data <- as.xts(read.zoo("data1.csv",sep=",",tz="" ,header=T)) index(data) <- as.Date(index(data)) . Index of data it gives as "2010-01-03" – user1177819 Jan 04 '13 at 08:31

2 Answers2

3

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"
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • if I run the command as.Date(data) it gives correct date even in Linux. class(Index(data)) is POSIXct. When I change POSIXct to date object by using index(data) <- as.Date(index(data)) then it store the previous day date in index(data) – user1177819 Jan 08 '13 at 09:53
1
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?

Arun
  • 116,683
  • 26
  • 284
  • 387
  • First change this data frame to time series by using below command data <- as.xts(read.zoo("data1.csv",sep=",",tz="" ,header=T)) here index(data) produce "2010-01-04" "2010-01-04" then do index(data) <- as.Date(index(data)) here is gives output as "2010-01-03" "2010-01-03". This problem occurs in linux only. Same code works on window. – user1177819 Jan 04 '13 at 08:40
  • What do you mean by locale and How to check that. I checked date() in linux and window and it gives current time and date – user1177819 Jan 04 '13 at 08:51
  • Checked everything.ALl local settings are correct. There is some other problem – user1177819 Jan 04 '13 at 09:46
  • Sys.getlocale() in window gives: "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" in Linux gives: "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C" – user1177819 Jan 04 '13 at 09:53
  • Problem not sorted out yet. Any other idea – user1177819 Jan 04 '13 at 10:38