jonnie
jonnie

Reputation: 765

handling matrices with rows of unequal length in R

There are two matrices that I want to divide: numer1 and denom1. The problem is that they are of unequal row lengths. The script is run every week, so the dimensions change weekly, too.

This week:

dim(numer1) = 998 rows, 99 columns 

dim(denom1) = 997 rows, 99 columns. 

Last week:

dim(numer1) = 999 rows, 99 columns 

dim(denom1) = 998 rows, 99 columns.

Is there a way to compare these matrices and remove the last row in the larger matrix (in this example, numer1)?

Here's what I have tried:

fun1 <- as.data.frame(abs(numer1[-last(numer1),]/denom1))

Thank you!

Upvotes: 0

Views: 550

Answers (1)

seancarmody
seancarmody

Reputation: 6290

How about this:

rows <- 1:pmin(nrow(numer1), nrow(denom1))
frac1 <- numer1[rows,] / denom1[rows,]

Upvotes: 1

Related Questions