[R-SIG-Finance] using "to.weekly" on a zoo object

Brian G. Peterson brian at braverock.com
Wed Apr 7 11:12:46 CEST 2010


Hodgess, Erin wrote:
> Dear Finance People:
> 
> I used the "get.hist.quote" to get the S&P as seen below:
> 
>>  spc <- get.hist.quote(instrument = "^gspc", start = "1998-01-01",
> +               quote = "Close")
> trying URL 'http://chart.yahoo.com/table.csv?s=^gspc&a=0&b=01&c=1998&d=3&e=05&f=2010&g=d&q=q&y=0&z=^gspc&x=.csv'
> Content type 'text/csv' length unknown
> opened URL
> .......... .......... .......... .......... ..........
> .......... .......... .......... .......... ..........
> .......... .......... .......... .......... ..........
> .......... .......... .......... ...
> downloaded 183 Kb
> 
> time series starts 1998-01-02
> 
> 
> Then I use the to.weekly to convert to weekly data:
> 
>> spc.w <- to.weekly(spc)
>> str(spc.w)
> An ‘xts’ object from 1998-01-02 to 2010-04-05 containing:
>   Data: num [1:641, 1:4] 975 977 939 979 957 ...
>  - attr(*, "dimnames")=List of 2
>   ..$ : NULL
>   ..$ : chr [1:4] "spc.Open" "spc.High" "spc.Low" "spc.Close"
>   Indexed by objects of class: [Date] TZ: 
>   Original class: 'zoo'  
>   xts Attributes:  
>  NULL
> 
> My question is:  I  only need the "Close" section.  The other ones (Open, High, Low) are duplicates.  Is there a way just to return the Close component, please?

spc.wc <- Cl(to.weekly(spc))
?Cl

# or

spc.wc <- to.weekly(spc)[,"spc.Close"]

# or

spc.wc <- Cl(spc[endpoints(spc,on='weeks'),])
?endpoints


There is quite a lot of data in the list archives on to.period and endpoints. 
  Also, subsetting R objects is a FAQ, covered in any good introduction to R.

Regards,

     - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list