Suppose I have a two column matrix. How do I pack the columns into a pair/tuple so that they can be assigned to a one column matrix?
> A = matrix(NA,nrow=5,ncol=1)
> B = matrix(runif(10),ncol=2)
> A
[,1]
[1,] NA
[2,] NA
[3,] NA
[4,] NA
[5,] NA
> B
[,1] [,2]
[1,] 0.1886287 0.6995596
[2,] 0.1576875 0.9792369
[3,] 0.9056386 0.1640904
[4,] 0.9125812 0.7003167
[5,] 0.9327778 0.8149431
> A[,1] = B # need this to work
I have a n-col matrix of prices, a column for each stock. I am trying to compute a moving MACD statistic for each stock. I am using a n-col MACD matrix to contain the results. When I feed a one col of prices to MACD function (from package TTR), it returns a 2-col matrix of signal and macd, so I need to way to contain this statistic within the same dimension.
You can do that with lists.
That said, this is a very un-R-like way to do things and is probably more trouble than it’s worth. If you describe what you’re actually trying to do, someone could show you a more appropriate approach.
UPDATE:
Based on the updated question, the code below will put the
macdandsignalin a 2n matrix. You may want to write a more elaborate function (e.g. one that identifies themacdandsignalcolumns with their respective instruments).If you want the
macdandsignalcolumns in separate matrices, you could justgrepthe columns from theoutobject.