将数据从CSV文件转换为“ XTS”对象
-
16-10-2019 - |
题
我有CSV文件,该文件具有以下格式的日期:2004年8月25日
我想将其读为“ XTS”对象,以便在QuantMod软件包中使用函数“ pecurturn”。
我可以将以下文件用于该功能吗?
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
引导我也是如此。
解决方案
不幸的是我不能说 ts
部分,但这是您可以将日期转换为适当格式的方式,该格式可以通过其他函数(或时间)读取。您可以像往常一样将数据导入到数据中。在这里查看您是否错过了)。然后,您可以转换您的 Date
列成A。 POSIXlt
(POSIXt
)使用 strptime
功能。
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
其他提示
尝试这个。我们摆脱了滋扰列,并指定时间索引的格式,然后转换为XTS并应用每日撤销功能:
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)
当然, textConnection(Lines)
只是为了保持榜样自我包含,实际上将被类似的榜样取代 "myfile.dat"
.
不隶属于 StackOverflow