Skip to content

R-help with apply and ccf

3 messages · Michael Andric, ONKELINX, Thierry, Bert Gunter

#
You could combine them with cbind, and then split the rows again inside
the function you're calling with apply.

Mat <- cbind(mat1, mat2)
apply(Mat, 1, function(x){
	row.mat1 <- x[seq_len(length(x)/2)]
	row.mat2 <- x[length(x)/2 + seq_len(length(x)/2)]
	cor(row.mat1, row.mat2)
})

Cheers,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney
#
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

Michael


______________________________________________
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.