2016-10-14 7 views
0
rm(list = ls(all = TRUE)) 

library(timeSeries) 
library(timeDate) 
library(PerformanceAnalytics) 
library(quantmod) 
library(xts) 
library(zoo) 
library(tseries) 

sessionInfo() 

################################################################################# 
R version 3.3.1 (2016-06-21) 
Platform: x86_64-apple-darwin13.4.0 (64-bit) 
Running under: OS X 10.12 (Sierra) 

locale: 
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8 

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] tseries_0.10-35    quantmod_0.4-6    TTR_0.23-1     PerformanceAnalytics_1.4.3541 
[5] xts_0.9-7      zoo_1.7-13     timeSeries_3022.101.2   timeDate_3012.100    

loaded via a namespace (and not attached): 
[1] tools_3.3.1  grid_3.3.1  lattice_0.20-34 quadprog_1.5-5 
############################################################################### 

EURUSD <- read.csv(file="EURUSD-2016-08.csv", header=FALSE) #from TrueFX 
names(EURUSD)[names(EURUSD)=="V1"] <- "Symbol" 
names(EURUSD)[names(EURUSD)=="V2"] <- "Date" 
names(EURUSD)[names(EURUSD)=="V3"] <- "Bid" 
names(EURUSD)[names(EURUSD)=="V4"] <- "Offer" 
head(EURUSD) 

    Symbol     Date  Bid Offer 
1 EUR/USD 20160801 00:00:00.018 1.11672 1.11675 
2 EUR/USD 20160801 00:00:00.141 1.11672 1.11677 
3 EUR/USD 20160801 00:00:00.142 1.11671 1.11677 
4 EUR/USD 20160801 00:00:00.143 1.11671 1.11676 
5 EUR/USD 20160801 00:00:00.146 1.11672 1.11676 
6 EUR/USD 20160801 00:00:00.150 1.11673 1.11677 

class(EURUSD) 

[1] "data.frame" 

EURUSD <- EURUSD[,-1] 
options(digits.secs=3) 
EURUSD <- xts(EURUSD[, 2:3], order.by=as.POSIXct(EURUSD[,1], format="%Y%m%d %H:%M:%S")) 
head(EURUSD) 

       Bid Offer 
2016-08-01 1.11672 1.11675 
2016-08-01 1.11672 1.11677 
2016-08-01 1.11671 1.11677 
2016-08-01 1.11671 1.11676 
2016-08-01 1.11672 1.11676 
2016-08-01 1.11673 1.11677 

class(EURUSD) 

[1] "xts" "zoo" 

이 문제는 이전에이 블로그에서 다뤘지만 정확한 날짜를 얻지 못했습니다. 밀리 초 단위로 형식을 지정하십시오. 내가 어디로 잘못 가고 있니?POSIXct는 형식을 올바르게 입력 한 후에도 % H : % M : % S을 픽킹하지 않는 것 같습니다.

감사

다음
+2

이 http://stackoverflow.com/questions/2150138/how-to-parse-milliseconds-in-r –

+0

를 참조하십시오 사실 내가 가지고 format = "% Y % m % d % H : % M : % S"형식에서 코딩 오류가 발생합니다. % S 대신 % OS 여야합니다. 문제를 해결했습니다. – riskmaverick

답변

0

그것을 할 방법은 다음과 같습니다

options(digits.secs=3) 
test <- "20160801 00:00:00.018" 
as.POSIXct(test, format="%Y%m%d %H:%M:%OS")