[R] apply & incompatible dimensions error

Bernzweig, Bruce (Consultant) bbernzwe at bear.com
Tue Jul 24 17:05:25 CEST 2007


Hi,

I've created the following two matrices (mat1 and mat2) and a function
(f) to calculate the correlations between the two on a row by row basis.

	mat1 <- matrix(sample(1:500,50), ncol = 5, 
		dimnames=list(paste("row", 1:10, sep=""), 
		paste("col", 1:5, sep="")))

	mat2 <- matrix(sample(501:1000,50), ncol = 5, 
		dimnames=list(paste("row", 1:10, sep=""), 
		paste("col", 1:5, sep="")))

	f <- function(x,y) cor(x,y)

When the matrices are squares (# rows = # columns) I have no problems.

However, when they are not (as in the example above with 5 columns and
10 rows), I get the following error:

> apply(mat1, 1, f, y=mat2)
Error in cor(x, y, na.method, method == "kendall") : 
        incompatible dimensions

Any help would be appreciated.  Thanks!

- Bruce



**********************************************************************
Please be aware that, notwithstanding the fact that the pers...{{dropped}}



More information about the R-help mailing list