Pergunta

I have got CSV files which has the Date in the following format: 25-Aug-2004

I want to read it as an "xts" object so as to use the function "periodReturn" in quantmod package.

Can I use the following file for the function?

Symbol Series        Date Prev.Close Open.Price High.Price Low.Price

1       XXX     EQ 25-Aug-2004     850.00    1198.70    1198.70    979.00

2       XXX     EQ 26-Aug-2004     987.95     992.00     997.00    975.30

Guide me with the same.

Foi útil?

Solução

Unfortunately I can't speak for the ts part, but this is how you can convert your dates to a proper format that can be read by other functions as dates (or time). You can import your data into a data.frame as usual (see here if you've missed it). Then, you can convert your Date column into a POSIXlt (POSIXt) class using strptime function.

nibha <- "25-Aug-2004" # this should be your imported column
lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C") #temporarily change locale to C if you happen go get NAs
strptime(nibha, format = "%d-%b-%Y")
Sys.setlocale("LC_TIME", lct) #revert back to your locale

Outras dicas

Try this. We get rid of the nuisance columns and specify the format of the time index, then convert to xts and apply the dailyReturn function:

Lines <- "Symbol Series Date Prev.Close Open.Price High.Price Low.Price
1 XXX EQ 25-Aug-2004 850.00 1198.70 1198.70 979.00
2 XXX EQ 26-Aug-2004 987.95 992.00 997.00 975.30"

library(quantmod) # this also pulls in xts & zoo

z <- read.zoo(textConnection(Lines), format = "%d-%b-%Y",
    colClasses = rep(c(NA, "NULL", NA), c(1, 2, 5)))
x <- as.xts(z)
dailyReturn(x)

Of course, textConnection(Lines) is just to keep the example self contained and in reality would be replaced with something like "myfile.dat".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top