>require(quantmod)
>setSymbolLookup(SDB=list(name="000001.sz",src="yahoo"))
>getSymbols("SDB",from="2010-01-01",to="2010-01-10")
> SDB
000001.SZ.Open 000001.SZ.High 000001.SZ.Low 000001.SZ.Close
2010-01-04 24.52 24.58 23.68 23.71
2010-01-05 23.75 23.90 22.75 23.30
2010-01-06 23.25 23.25 22.72 22.90
2010-01-07 22.90 23.05 22.40 22.65
2010-01-08 22.50 22.75 22.35 22.60
我想要两种输出:
1.
> SDB
date open high low close
2010-01-04 24.52 24.58 23.68 23.71
2010-01-05 23.75 23.90 22.75 23.30
2010-01-06 23.25 23.25 22.72 22.90
2010-01-07 22.90 23.05 22.40 22.65
2010-01-08 22.50 22.75 22.35 22.60
2
> SDB
date open high low close
1 2010-01-04 24.52 24.58 23.68 23.71
2 2010-01-05 23.75 23.90 22.75 23.30
3 2010-01-06 23.25 23.25 22.72 22.90
4 2010-01-07 22.90 23.05 22.40 22.65
5 2010-01-08 22.50 22.75 22.35 22.60
我怎么能得到它?
答案 0 :(得分:2)
这是你想要的一个裂缝。我认为您的问题始于SDB的存储方式。要查看对象的外观,请使用str
。我不知道xts
对象是什么,但使用str
可以帮助我确定如何取出我想要的部分。
str(SDB)
> str(SDB)
An ‘xts’ object from 2010-01-04 to 2010-01-08 containing:
Data: num [1:5, 1:6] 24.5 23.8 23.2 22.9 22.5 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "000001.SZ.Open" "000001.SZ.High" "000001.SZ.Low" "000001.SZ.Close" ...
Indexed by objects of class: [Date] TZ:
xts Attributes:
List of 4
$ tclass : chr [1:2] "POSIXct" "POSIXt"
$ tzone : chr ""
$ src : chr "yahoo"
$ updated: POSIXct[1:1], format: "2012-07-29 08:33:57"
现在我们可以开始工作了:
#grab first 4 columns and make it into a dataframe
NEW <- data.frame(SDB[, 1:4])
#turn the rownames to a column
NEW <- data.frame(date=rownames(NEW), NEW)
#remove the junk and lowercases the first letter
colnames(NEW) <- sub('^(\\w?)', '\\L\\1',
gsub("X000001.SZ.", "", colnames(NEW)), perl=T)
#make rownames integer
rownames(NEW)<- NULL
#the two ways you wanted it to look
NEW
print(NEW ,row.names = FALSE)