问题描述
我有CSV文件,其日期具有以下格式:2004年8月25日
I have got CSV files which has the Date in the following format:25-Aug-2004
我想将其读取为"xts"对象,以便在quantmod程序包中使用函数"periodReturn".
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
用同样的方法指导我.
推荐答案
不幸的是,我不能说ts
部分,但这是将日期转换为其他函数可以读取的正确格式的方法作为日期(或时间).您可以像往常一样将数据导入到data.frame中(如果需要,请参见此处已经错过了).然后,您可以使用strptime
函数将Date
列转换为POSIXlt
(POSIXt
)类.
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
这篇关于将数据从csv文件转换为"xts"目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!