[R] apply & incompatible dimensions error
Benilton Carvalho
bcarvalh at jhsph.edu
Tue Jul 24 17:30:57 CEST 2007
are you positive that your function is doing what you expect it to do?
it looks like you want something like:
sapply(1:10, function(i) cor(mat1[i,], mat2[i,]))
b
On Jul 24, 2007, at 11:05 AM, Bernzweig, Bruce ((Consultant)) wrote:
> 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}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list