Hi Jake.
Sorry, I misunderstood about what you wanted.
Instead of this:
lapply(split(indx,(indx-1)%%n+1),function(i) mat1[,i])
If I use:
res1<- lapply(split(indx,(indx-1)%/%n+1),function(i) mat1[,i])
#or
lapply(split(indx, as.numeric(gl(ncol(mat1),n,ncol(mat1)))),function(i)
mat1[,i])
?lapply(res1,head,2)[1:2]
#$`1`
?# ??? O? H? L? C
#[1,] 18 20 30 20
#[2,] 14 15 15 45
#
#$`2`
?# ??? O? H? L? C
#[1,] 56? 6 25 13
#[2,] 31 37 23 17
A.K.
So, i got it worked out. Thanks for your input. I see that you used a
mod, which worked well for the application which you solved, and an
application that will likely come up again. Anyways, here is the
solution I was lookin for:
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,rep(1:6,each = 4, times = 1)),function(i) mat1[,i])
##rep(1:6,each = 4, times = 1)
## [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6
lapply(res,head,2)
$`1`
? ? ? O ?H ?L ?C
[1,] 18 20 30 20
[2,] 14 15 15 45
$`2`
? ? ? O ?H ?L ?C
[1,] 56 ?6 25 13
[2,] 31 37 23 17
$`3`
? ? ? O ?H ?L ?C
[1,] 51 ?4 29 ?8
[2,] 60 22 15 35
$`4`
? ? ? O ?H ?L ?C
[1,] 24 23 ?1 44
[2,] 12 52 10 ?8
$`5`
? ? ? O ?H ?L ?C
[1,] 24 10 57 ?5
[2,] 43 30 44 25
$`6`
? ? ? O ?H ?L ?C
[1,] 52 ?2 16 13
[2,] 34 42 60 12
Thanks again
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Thursday, September 26, 2013 5:15 PM
Subject: Re: Grouping Matrix by Columns; OHLC Data
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