[R] Multiple operations on list

Gabor Grothendieck ggrothendieck at myway.com
Mon Sep 20 20:41:42 CEST 2004



You could do it with a double sapply like this:

tr <- function(x) sum(diag(x))
sapply(a, function(x1) sapply(a, function(x2) 
  tr(x1%*%t(x1)%*%x2%*%t(x2)) / (tr(x1%*%t(x1))*tr(x2%*%t(x2)))))

Date:   	Mon, 20 Sep 2004 13:24:49 -0400
From:   	Stephane DRAY <stephane.dray at umontreal.ca>
To:   	<r-help at stat.math.ethz.ch>
Subject:   	[R] Multiple operations on list

Hello,

suppose I have a list with matrices:

a=list(x1=matrix(rnorm(10),5,2),x2=matrix(rnorm(10),5,2),x3=matrix(rnorm(10),5,2))

I want to compute for all combination of xi and xj (x1,x2 x1,x3 and x2,x3)
a value.
This value is given for the pair x1,x2 by trace(x1%*%t(x1)%*%x2%*%t(x2)) /
trace(x1%*%t(x1))*trace(x2%*%t(x2))

I know that product matrices t(xi)%*%xi can be obtained by:

aa=lapply(a,crossprod)

but I do not know how to "mix" the values in aa to obtain the desired values.

Is there a way to do it without for loop ?

Thanks in advances,

sincerely

Stéphane DRAY
--------------------------------------------------------------------------------------------------

Département des Sciences Biologiques
Université de Montréal, C.P. 6128, succursale centre-ville
Montréal, Québec H3C 3J7, Canada

Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293
E-mail : stephane.dray at umontreal.ca




More information about the R-help mailing list