[R] R-help with apply and ccf

Bert Gunter gunter.berton at gene.com
Tue May 22 18:35:32 CEST 2007

I understand you to want correlations of corresponding rows (** not ccf,
which returns a vector ccf for each pair of rows). If that is so, 

1) ... in theory, diag(cor(t(A), t(B)) would work without apply, except
196,000 rows is probably too large, and it is probably too inefficient to
compute and then throw away all the off-diagonals anyway.

2. ##Use a 3d array.
 ar <- array(c(A,B),dim=c(dim(A),2)) ## this can also be done by abind() in
the abind package
  apply(ar,1,function(x)cor(x[,1],x[,2])) ## Value is a vector

3. ## probably simplest and best
 sapply(seq_along(nrow(a)),function(i)cor(a[i,],b[i,])) ## Note: value is a
vector, not an array

Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Michael Andric
Sent: Tuesday, May 22, 2007 8:35 AM
To: r-help at stat.math.ethz.ch
Subject: [R] R-help with apply and ccf

Dear R gurus,

I would like to use the ccf function on two matrices that are each 196000 x
12.  Ideally, I want to be able to go row by row for the two matrices using
apply for the ccf function and get one 196000 X 1 array output.  The apply
function though wants only one array, no?  Basically, is there a way to use
apply when there are two arrays in order to do something like correlation on
a row by row basis?
Thanks for your help


	[[alternative HTML version deleted]]

R-help at stat.math.ethz.ch mailing list
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