[R-SIG-Finance] Computation on xts

Joshua Ulrich josh.m.ulrich at gmail.com
Sat Apr 30 20:53:02 CEST 2011


Hi Robert,

On Sat, Apr 30, 2011 at 12:39 AM,  <me at censix.com> wrote:
>
>> Hi,
>>
>> I have the following time series in xts
>>
>>> getSymbols("SBUX");
>> [1] "SBUX"
>> Warning message:
>> In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  :
>>   downloaded length 53650 != reported length 200
>>
>> Then I tried to compute something row-wise. In my below example, I
>> simple compute daily notional using close price. I want to add the
>> result back as a new column in my existing time series. But it doesn't
>> seem to work. I'm not sure if I have missed anything. Thank you.
>>
>>>SBUX[,"TurnOver"] <- SBUX[,"SBUX.Close"]*SBUX[,"SBUX.Volume"]
>> Error in NextMethod(.Generic) : subscript out of bounds
>>
>> Cheers,
>>
>> Robert
>>
>>
>
> Hi,
>
> I guess one way to do it would be
>
>   SBUX <-cbind(SBUX, SBUX[,"SBUX.Close"]*SBUX[,"SBUX.Volume"])
>
> and then you can rename the new column using colnames
>
>   colnames(SBUX) <- c("SBUX.Open" , "SBUX.High"  ,   "SBUX.Low" ,
> "SBUX.Close" , "SBUX.Volume" ,  "SBUX.Adjusted", "TurnOver" )
>
>
> Cheers
>
> Soren
>
> --
> http://censix.com
>

Another way is via the `$<-` operator:

SBUX$TurnOver <- SBUX[,"SBUX.Close"]*SBUX[,"SBUX.Volume"]

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



More information about the R-SIG-Finance mailing list