Here’s a crack at what you want. I think your problem starts with how SDB is stored. To see what an object looks like use str
. I don’t know what an xts
object is but using str
helps me determine how to pull out the pieces I want.
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 "https://stackoverflow.com/questions/11709064/yahoo"
$ updated: POSIXct[1:1], format: "2012-07-29 08:33:57"
Now we can get to work:
#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)
4
solved to get data.frame from xts form data [closed]