Skip to content
Prev 313821 / 398503 Next

R beginner: matrix algebra

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