[R] Grouping Matrix by Columns; OHLC Data

arun smartpink111 at yahoo.com
Thu Sep 26 23:15:14 CEST 2013


HI,
May be this helps:

set.seed(24)
 mat1<- matrix(sample(1:60,30*24,replace=TRUE),ncol=24)
colnames(mat1)<- rep(c("O","H","L","C"),6)
indx<-seq_along(colnames(mat1))
n<- length(unique(colnames(mat1)))
 res<- lapply(split(indx,(indx-1)%%n+1),function(i) mat1[,i])
lapply(res,head,2)
#$`1`
#      O  O  O  O  O  O
#[1,] 18 56 51 24 24 52
#[2,] 14 31 60 12 43 34
#
#$`2`
#      H  H  H  H  H  H
#[1,] 20  6  4 23 10  2
#[2,] 15 37 22 52 30 42
#
#$`3`
#      L  L  L  L  L  L
#[1,] 30 25 29  1 57 16
#[2,] 15 23 15 10 44 60
#
#$`4`
#      C  C  C  C  C  C
#[1,] 20 13  8 44  5 13
#[2,] 45 17 35  8 25 12

A.K.



Motivation: 

Bring in data containing a number of columns divisable by 4. 
This data contains several different assets and the columns correspond 
to Open,High,Low,Close, ....Open,High,Low,Close,  etc (thus divisible by
 4). From where I am getting this data, the header is not labled as 
Open,High,Low,Close, but rather just has the asset symbol. 

The end goal is to have each Open,High,Low,Close,  as its own 
OHLC object, to be run through different volatility functions (via 
QuantMod ) 

I believe i am best served by first grouping the original data 
so that each asset is its own object, with 4 columns. Then i can rename 
the columns to be: 
colnames(function$asset) <-c("Open", "High","Low", "Close") 

I've attempted to use split, but am having trouble with split along the columns. 

Obviously I could manipulate the indexing, with something like 
data[i:i+4] and use a loop. Maybe this indexing approach would work with
 use of apply(). 


Previously, I've been using Mathematica for most of my data 
manipulation, and there I would partition the entire data set i.e. 
Matrix, into   column# / 4 separate objects.  So, in that case I have a 3
 dimensional object. I'd then call the object by its 3rd dimension index
 # [][#]. 

I'm having trouble doing that here. Any thoughts, or at the least  helping me to group the data by column. 

For the sake of possible examples, lets say the dimensions of my data is n.rows = 30, n.col = 24 




More information about the R-help mailing list