Skip to content

R beginner: matrix algebra

5 messages · Richard M. Heiberger, William Dunlap, Patrick Burns +1 more

#
Hi, I have an n x m matrix of numerical observations. ie. stock prices

I wish to convert the matrix of observations to a matrix of simple returns
(by taking the differences between (column) observations.)

Can any good soul suggest a function for this? 



--
View this message in context: http://r.789695.n4.nabble.com/R-beginner-matrix-algebra-tp4653335.html
Sent from the R help mailing list archive at Nabble.com.
#
diff(x) will returns the diff's of each column when x is a matrix, so there is no need for the apply call.
E.g.,
SQRE CUBE
2012 Q2    1    1
2012 Q3    4    8
2012 Q4    9   27
2013 Q1   16   64
2013 Q2   25  125
2013 Q3   36  216
2013 Q4   49  343
SQRE CUBE
2012 Q3    3    7
2012 Q4    5   19
2013 Q1    7   37
2013 Q2    9   61
2013 Q3   11   91
2013 Q4   13  127

filter() will do it also and may be more convenient because its output is the same length as its input:
[,1] [,2]
2012 Q2   NA   NA
2012 Q3    3    7
2012 Q4    5   19
2013 Q1    7   37
2013 Q2    9   61
2013 Q3   11   91
2013 Q4   13  127

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
Convenient ways of computing both simple
and log returns are at the very end of:

http://www.portfolioprobe.com/2012/11/05/an-easy-mistake-with-returns/

Those work whether you have a vector or
a matrix.

Pat
On 17/12/2012 17:16, kevj1980 wrote: