2011-12-17 38 views
2

我有下面的格式一系列次數據幀:[R舍入時間

08:09:23.079 

> class(timer3) 
[1] "factor" 

我想圓/把它們轉換成這種格式:

08:09 

結束目標是使用它們作爲plot的x軸的值,所以我假設它們需要去某種類型的時間格式(動物園,as.Date等)。

有什麼建議嗎?

+1

兩個步驟: 1)係數爲字符:as.character() 2)字符POSIXct:strptime() – mweylandt 2011-12-17 23:16:20

+1

轉換爲字符,並在'strptime看( )' – Chase 2011-12-17 23:19:01

+0

你們中的一個人想要在回答中打你的評論,以便我可以選擇它? – screechOwl 2011-12-21 21:37:00

回答

0

兩個步驟:1)係數爲字符:as.character()2)字符POSIXct:strptime()

2
as.zoo(sapply(timer3,substring,1,5)) 
or as.xts? 

也許看一個更大的數據樣本會有所幫助。

3

假設我們有這樣的輸入數據:

DF <- data.frame(times = c("08:09:23.079", "08:30:13.062"), values = 1:2) 

爲了簡單起見讓我們假設有以每分鐘最多一個時間點(我們展示的替代方案,是稍長之後如果沒有此限制) :

library(zoo) 
library(chron) 

# this assumes we want to store times to the second 
tt <- times(as.character(DF$times)) 
z <- zoo(DF$values, tt) 

plot(z, xaxt = "n") 

# custom axis - assumes sufficiently many points to get reasonable graph 
# round tick mark locations to the minute and remove the seconds from label 
axt <- trunc(times(axTicks(1)), "min") 
axis(1, at = axt, lab = sub(":..$", "", axt)) 

以上創建z的方法可以替換爲此。它適用於是否每分鐘超過一分,因爲它將它們聚合到一分鐘:

# with this z we will be store times to the minute 
z <- read.zoo(DF, FUN = function(x) trunc(times(as.character(x)), "min"), 
     aggregate = mean) 

編輯:繪圖和截斷。

2

在被稱爲巫師風險,因爲我認爲出現這種情況相當,我會回答這個問題經常。

以下是如何將您的時間序列數據轉換爲xts格式。這裏要使用的功能是align.time

> head(GBPJPY) 
        GBPJPY.Open GBPJPY.High GBPJPY.Low GBPJPY.Close 
2009-05-01 00:14:59  146.387  146.882 146.321  146.620 
2009-05-01 00:29:54  146.623  146.641 146.434  146.579 
2009-05-01 00:44:59  146.579  146.908 146.570  146.810 
2009-05-01 00:59:59  146.810  146.842 146.030  146.130 
2009-05-01 01:14:59  146.130  146.330 146.100  146.315 
2009-05-01 01:29:57  146.315  146.382 146.159  146.201 
> head(align.time(GBPJPY, 15*60)) 
        GBPJPY.Open GBPJPY.High GBPJPY.Low GBPJPY.Close 
2009-05-01 00:15:00  146.387  146.882 146.321  146.620 
2009-05-01 00:30:00  146.623  146.641 146.434  146.579 
2009-05-01 00:45:00  146.579  146.908 146.570  146.810 
2009-05-01 01:00:00  146.810  146.842 146.030  146.130 
2009-05-01 01:15:00  146.130  146.330 146.100  146.315 
2009-05-01 01:30:00  146.315  146.382 146.159  146.201