2012-07-30 44 views
-2

我有一些日期格式如下:每行如何更改日期格式中的R

 V1 V2 V3 
1 20100420 915 120 
2 20100420 920 150 
3 20100420 925 270 
4 20100420 1530 281 

3列,在1行意味着:2010-04-20 09:15 120

現在

我想將其更改爲1列(時間序列):

    V3 
1 20100420 09:15 120 
2 20100420 09:20 150 
3 20100420 09:25 270 
4 20100420 15:30 281 

或:

    V3 
1 20100420 9:15 120 
2 20100420 9:20 150 
3 20100420 9:25 270 
4 20100420 15:30 281 

我怎麼能在R中實現它?

+0

你有什麼試過的?這在R文檔和關於SO的不同問題上有很好的文檔記錄。 – A5C1D2H2I1M1N2O1R2T1 2012-07-30 05:57:33

+0

爲了澄清,在原始數據中,什麼是'V3'? – A5C1D2H2I1M1N2O1R2T1 2012-07-30 06:15:25

+0

它可能是股票的一些數據,前兩列是日期,最後一列可能是'open.price','close.price','volumn'等。 – PepsiCo 2012-07-30 06:21:57

回答

5

?strptime?sprintf是你的朋友:

重新創建數據集:

test <- read.table(textConnection("V1 V2 V3 
20100420 915 120 
20100420 920 150 
20100420 925 270"),header=TRUE) 

做一些粘貼:

strptime(
paste(
    test$V1, 
    sprintf("%04d", test$V2), 
    sep="" 
), 
format="%Y%m%d%H%M" 
) 

結果:

[1] "2010-04-20 09:15:00" "2010-04-20 09:20:00" "2010-04-20 09:25:00" 
+0

謝謝,函數'strptime'真的很有幫助!我在第一時間處理時間序列數據,看起來很複雜。 – PepsiCo 2012-07-30 06:55:45

+0

@thelatemail,所有'substr'的​​東西並不是真的必要,是嗎?只要最終字符串的長度是正確的,'strptime'應該能夠解析日期,而不必將短劃線嵌入到字符串中。 – A5C1D2H2I1M1N2O1R2T1 2012-07-30 07:05:58

+0

@mrdwab - 是的,你是對的。當我不再嘗試在手機上輸入時,我會將其修復爲僅引用測試$ V1而不是子字符串。 – thelatemail 2012-07-30 07:18:14

3

首先,解決您的格式和使用包像xts得到一個合適的時間序列對象:

# Read in the data. In the future, use `dput` or something else 
# so that others can read in the data in a more convenient way 
temp = read.table(header=TRUE, text=" V1 V2 V3 
1 20100420 915 120 
2 20100420 920 150 
3 20100420 925 270 
4 20100420 1530 281") 

# Get your date object and format it to a date/time object 
date = paste0(temp[[1]], apply(temp[2], 1, function(x) sprintf("%04.f", x))) 
date = strptime(date, format="%Y%m%d%H%M") 

# Extract just the values 
values = temp[[3]] 

# Load the xts package and convert your dataset 
require(xts) 
xts(values, order.by=date) 
#      [,1] 
# 2010-04-20 09:15:00 120 
# 2010-04-20 09:20:00 150 
# 2010-04-20 09:25:00 270 
# 2010-04-20 15:30:00 281 

在日期轉換:

  • apply(temp[2], 1, ...)去逐行對於臨時的第二列和將數字重新格式化爲四位數字。
  • 然後,paste0將所有的日期 - 時間信息結合到單個向量。
  • 最後,strptime將該字符向量轉換爲適當的日期時間對象。

更新

當然,如果你想只是一個普通data.frame,你也可以這樣做,但我強烈建議,如果你想要做真正的時間序列分析使用類似zooxts

下面是簡單的data.frame步驟(在之前創建datevalues對象之後)。

data.frame(V3 = values, row.names=date) 
#      V3 
# 2010-04-20 09:15:00 120 
# 2010-04-20 09:20:00 150 
# 2010-04-20 09:25:00 270 
# 2010-04-20 15:30:00 281 
+0

謝謝!我必須花更多的時間在它上面。 – PepsiCo 2012-07-30 07:18:14

+0

現在發佈後發佈的「DUH」時刻......簡化爲一行。對於'xts':'xts(temp $ V3,order.by = strptime(paste0(temp [[1]],sprintf(「%04.f」,temp [[2]])),format =「%Y (V3 = temp $ V3,row.names = strptime(paste0(temp [[1]],sprintf(「%m%d%H%M」))''和'data.frame': %04.f「,temp [[2]])),format =」%Y%m%d%H%M「))'。 – A5C1D2H2I1M1N2O1R2T1 2012-07-30 07:39:18