-8
>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

i want two kinds of output:
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

how can i get it?

Peng Peng
  • 1,305
  • 3
  • 16
  • 20
  • 4
    To me your two kinds of out put look identical other than one with row names and one without. I think you'll need to be more specific on what you want and intended use in order for people to provide you with more refined solution(s). – Tyler Rinker Jul 29 '12 at 12:33

1 Answers1

2

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 "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) 
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • > names(SDB)=c("open","high","low","close","volume","adjusted") > newdf = data.frame(date=index(SDB),SDB,row.names=NULL) why i can't combine the two lines into one line? – Peng Peng Jul 29 '12 at 13:16
  • i have runned your code ,it's ok . – Peng Peng Jul 29 '12 at 23:16
  • @PengPeng, `xts` objects have an `index` instead of rownames, so you can use `data.frame(date = index(SDB), data.frame(SDB), row.names=NULL)` to get a basic `data.frame`. See also [this question](http://stackoverflow.com/questions/11429889/converting-an-xts-object-to-a-data-frame). – A5C1D2H2I1M1N2O1R2T1 Jul 30 '12 at 03:02
  • @TylerRinker, this would be my approach: `temp = data.frame(date = index(SDB), data.frame(SDB)[1:4], row.names=NULL); names(temp) = tolower(gsub("X000001.SZ.", "", names(temp)))`. – A5C1D2H2I1M1N2O1R2T1 Jul 30 '12 at 03:19