[R-SIG-Finance] column transposition with xts

Aleks Clark aleks.clark at gmail.com
Wed Sep 16 12:56:54 CEST 2009


Josh,

was hoping to avoid a for loop, but I guess this is as good as it gets
:) luckily I should only have to run this once.

thanks,

Aleks

On Mon, Sep 14, 2009 at 9:52 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
>
> Aleks,
>
> This isn't the most elegant solution, especially with large data sets,
> but it is *a* solution.  The code below yields a 24-column xts object
> (20 lags of close prices, plus the original OHLC series).
>
> library(xts)
> library(quantmod)
>
> data(sample_matrix)
> x <- as.xts(sample_matrix[1:50,])
>
> res <- x
> for(i in 1:20) { res <- cbind(res,lag(Cl(x),i)) }
>
> HTH,
> Josh
> --
> http://www.fosstrading.com
>
>
>
> On Mon, Sep 14, 2009 at 5:53 AM, Aleks Clark <aleks.clark at gmail.com> wrote:
> > I'm working with SVMs and have recently migrated to using TTR and thus xts,
> > but I've run into the following problem:
> >
> > For each vector in an SVMs training set, I need to have a given amount of
> > historical data available as features for the vector. A simple example would
> > be to have the past 20 close prices. So I'd take a matrix like this:
> >    O H L C
> > 1
> > 2
> > 3
> > 4
> > 5
> > ...
> > N
> >
> > and output something like this to give to my SVM:
> >
> > O, H, L, C, C-1, C-2, C-3, etc
> > 1
> > 2
> > 3
> > 4
> > 5
> >
> > obviously at the top of the matrix, the historical Close cells would be
> > empty until there was enough data. In the pre-xts days, I accomplished this
> > using sapply() and t(), a probably hackish solution, but all I was able to
> > come up with. However, I am now running into problems with indices using
> > this technique, and was wondering if there was a better way to perform this
> > transformation, which really only amounts to a simple copy of data. Possible
> > solutions that occur to me include some sort of "index shift" and then a
> > merge, but I've been unable to get anything to work.
> >
> >
> > --
> > Aleks Clark
> >
> >        [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > -- Subscriber-posting only.
> > -- If you want to post, subscribe first.
> >



--
Aleks Clark



More information about the R-SIG-Finance mailing list