Steffen
Steffen

Reputation: 3

R: How can I obtain returns by row in a matrix?

First I create a 5x4 matrix with random numbers from 1 to 10:

A <- matrix(sample(1:10, 20, TRUE), 5, 4)
> A
     [,1] [,2] [,3] [,4]
[1,]    1    5    6    6
[2,]    5    9    9    4
[3,]   10    6    1    8
[4,]    4    4   10    2
[5,]   10    9    7    5

In the following step I would like to obtain the returns by row (for row 1: (5-1)/1, (6-5)/5, (6-6)/6 and the same procedure for the other rows). The final matrix should therefore be a 5x3 matrix.

Upvotes: 0

Views: 272

Answers (2)

fabla
fabla

Reputation: 1816

You can make use of the Base R funtion diff() applied to your transposed matrix:

Code:

# Data
set.seed(1)

A <- matrix(sample(1:10, 20, TRUE), 5, 4)

#     [,1] [,2] [,3] [,4]
#[1,]    9    7    5    9
#[2,]    4    2   10    5
#[3,]    7    3    6    5
#[4,]    1    1   10    9
#[5,]    2    5    7    9

# transpose so we get per row and not column returns 

t(diff(t(A))) / A[, -ncol(A)]

           [,1]       [,2]       [,3]
[1,] -0.2222222 -0.2857143  0.8000000
[2,] -0.5000000  4.0000000 -0.5000000
[3,] -0.5714286  1.0000000 -0.1666667
[4,]  0.0000000  9.0000000 -0.1000000
[5,]  1.5000000  0.4000000  0.2857143

Upvotes: 1

dspn
dspn

Reputation: 314

A <- matrix(sample(1:10, 20, TRUE), 5, 4)

fn.Calc <- function(a,b){(a-b)/a}
B <- matrix(NA, nrow(A), ncol(A)-1)
for (ir in 1:nrow(B)){
    for (ic in 1:ncol(B)){
        B[ir, ic] <- fn.Calc(A[ir, ic+1], A[ir, ic])
    }    
}

small note: when working with random functions providing a seed is welcomed ;)

So what we have here: fn.Calc is just the calculation you are trying to do, i've isolated it in a function so that it's easier to change if needed

then a new B matrix is created having 1 column less then A but the same rows

finally we are going to loop every element in this B matrix, I like to use ir standing for incremental rows and ic standing for incremental column and finally inside the loop (B[ir, ic] <- fn.Calc(A[ir, ic+1], A[ir, ic])) is when the magic happens where the actual values are calculated and stored in B

it's a very basic approach without calling any package, there's probably many other ways to solve this that require less code.

Upvotes: 0

Related Questions