2013-08-27 1 views
2

Google에서 1 분 작은 따옴표를 가져옵니다. 데이터를 처리 한 후 1 분 간격으로 xts 객체를 만들려고했지만 동일한 datetime이 여러 번 반복되었지만 이유를 이해하지 못합니다. my.dat2이라는 타임 스탬프 벡터를 만들기 위해 동일한 데이터를 사용하는 경우 작동합니다. 분 간격으로 Xts 개체를 만들 때 예기치 않은 날짜 형식이

library(xts) 
url <- 'https://www.google.com/finance/getprices?q=IBM&i=60&p=15d&f=d,o,h,l,c,v' 
x <- read.table(url,stringsAsFactors = F) 
mynam <- unlist(strsplit(unlist(strsplit(x[5,], split='=', fixed=TRUE))[2] , split=',')) 
interv <- as.numeric(unlist(strsplit(x[4,], split='=', fixed=TRUE))[2]) 

x2 <- do.call(rbind,strsplit(x[-(1:7),1],split=',')) 
rownames(x2) <- NULL 
colnames(x2) <- mynam 

ind <- which(nchar(x2[,1])>5) 
x2[ind,1] <- unlist(strsplit(x2[ind,1], split='a', fixed=TRUE))[2] 
#To convert from data.frame to numeric 
class(x2) <- 'numeric' 

my.dat <- rep(0,nrow(x2)) 
#Convert all to same format 
for (i in 1:nrow(x2)) { 
    if (nchar(x2[i,1])>5) { 
    ini.dat <- x2[i,1] 
    my.dat[i] <- ini.dat 
    } else { 
    my.dat[i] <- ini.dat+interv*x2[i,1] 
    } 
} 

df <- xts(x2[,-1],as.POSIXlt(my.dat, origin = '1970-01-01')) 
head(df,20) 

my.dat2 <- as.POSIXlt(my.dat, origin = '1970-01-01') 
head(my.dat2,20) 

나는 데이터를 시뮬레이션하고 XTS 개체를 만들 수있는 분으로 날짜의 시퀀스를 생성하는 간단한 예를 시도하고 그래서는 XTS 기능에 날짜를 통과 할 때 저는 누락 된 것이어야했다.

답변

1

my.dat 개체에 중복 값이 ​​있고 xts 및 zoo 개체를 정렬해야하므로 모든 중복 값이 ​​함께 그룹화됩니다.

문제는 모든 비어 있지 않은 요소가 아닌 두 번째 요소 만 사용하는이 줄입니다.

x2[ind,1] <- unlist(strsplit(x2[ind,1], split='a', fixed=TRUE))[2] 
# this should be 
x2[ind,1] <- sapply(strsplit(x2[ind,1], split='a', fixed=TRUE), "[[", 2)