2013-05-31 35 views
0

我有一個很大的數據集,前兩列包含字符類型的日期/時間信息。Chron to POSIXct

 Date  Time     Current 
1 28.02.2013 23:58:50      NA 
2 28.02.2013 23:58:50      0.3 
3 28.02.2013 23:58:51      0.3 
4 28.02.2013 23:58:51      0.3 
5 28.02.2013 23:58:57      0.3 
6 28.02.2013 23:58:58      0.3 

因爲我想將它轉換爲XTS對象以後,我把這些兩列到包含日期和時間用下面的代碼類型POSIXct之一:

bigdata <- ldply (files, read.table,header=TRUE, sep = "\t", dec=".", as.is= 1:2) #read the tables keeping the charater format of the first two columns 
library("chron") 
Time<-chron(bigdata$Date, bigdata$Time, format= c(dates= "d.m.y", times="h:m:s")) 
Time2<- as.POSIXct(paste(as.Date(dates(Time)),times(Time)%%1)) #to POSIXct 

結果在時間2是與日期一POSIXct對象和在單個列中的時間,並得到所需的結果:

"2013-02-01 00:02:09 CET" "2013-02-01 00:02:12 CET" "2013-02-01 00:02:13 CET" "2013-02-01 00:02:13 CET" "2013-02-01 00:02:13 CET"..... 

(我知道,因爲我使用的前一結果的時間是不同的,但想法是

但是,對於一些奇怪的原因,具有相同的特性一者的一些數據集我之前表現出相同的),我得到了時間2的結果原來是:

"2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET" 

(此時間與上述數據集中提取的時間相同)

「時間」部分被完全刪除,只剩下日期。我已經將相同的代碼應用於各種數據集,並且大多數情況下它工作得很好,但是其中一個出現了這個問題。

爲什麼我會得到不同的結果?

---------------------------編輯------------------ ---------------------

我已經在這兩個運行命令dput對象時間和時2.赫拉結果如下:

> dput(head(Time2)) 
structure(c(1362006000, 1362006000, 1362006000, 1362006000, 1362006000, 
1362006000), class = c("POSIXct", "POSIXt"), tzone = "") 

> dput(head(Time)) 
structure(c(15764.9991898148, 15764.9991898148, 15764.9992013889, 
15764.9992013889, 15764.9992708333, 15764.9992824074), format = structure(c("d.m.y", 
"h:m:s"), .Names = c("dates", "times")), origin = structure(c(1, 
1, 1970), .Names = c("month", "day", "year")), class = c("chron", 
"dates", "times")) 
+1

需要dput(head(problem_object))的輸出。 –

+0

你問我在對象Time2中運行命令並將結果添加到原始問題 – Tarigarma

回答

0

爲什麼不相反只是在一個操作中做,而不是通過一個chron-class對象?

Time2<-as.POSIXct(paste(bigdata$Date, bigdata$Time), 
        format= "%d.%m.%Y %H:%M:%S") 

########## 
bigdata <- read.table(text=" Date  Time     Current 
1 28.02.2013 23:58:50      NA 
2 28.02.2013 23:58:50      0.3 
3 28.02.2013 23:58:51      0.3 
4 28.02.2013 23:58:51      0.3 
5 28.02.2013 23:58:57      0.3 
6 28.02.2013 23:58:58      0.3", header=TRUE, 
     stringsAsFactors=FALSE) 

library("chron") 
Time<-chron(bigdata$Date, bigdata$Time, format= c(dates= "d.m.y", times="h:m:s")) 
Time2<- as.POSIXct(paste(as.Date(dates(Time)),times(Time)%%1)) 
Time2 
[1] "2013-02-28 23:58:50 PST" "2013-02-28 23:58:50 PST" "2013-02-28 23:58:51 PST" 
[4] "2013-02-28 23:58:51 PST" "2013-02-28 23:58:57 PST" "2013-02-28 23:58:58 PST"