unexpected date format when creating a xts object with minute intervals in R

StackOverflow https://stackoverflow.com/questions/18471776

  •  26-06-2022
  •  | 
  •  

문제

I'm retrieving one minute quotes from google. After processing the data I try to create an xts object with one minute intervals but get same datetime repeated several times but don't understand why. Note that if I use the same data to build a vector of timestamps called my.dat2it does work.

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)

I tried a simpler example simulating the data and creating a sequence of dates by minute to create the xts object and it worked so it must be something that I'm missing when passing the dates to the xts function.

도움이 되었습니까?

해결책

Your my.dat object has duplicated values and xts and zoo objects must be ordered, so all the duplicate values are being grouped together.

The issue is this line, where you only take the second element, rather than every non-blank element.

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top