gpt4 book ai didi

r - 将数据从 csv 文件转换为 "xts"对象

转载 作者:行者123 更新时间:2023-12-04 12:26:31 25 4
gpt4 key购买 nike

我有 CSV 文件,其日期格式如下:
2004 年 8 月 25 日

我想将其作为“xts”对象读取,以便在 quantmod 包中使用“periodReturn”函数。

我可以将以下文件用于该功能吗?

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部分,但这是您如何将日期转换为可以被其他函数读取为日期(或时间)的正确格式的方法。
您可以像往常一样将数据导入 data.frame ( see here if you've missed it )。然后,您可以转换您的 Date列成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

关于r - 将数据从 csv 文件转换为 "xts"对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4756015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com