[R] Fwd: Empty list to use in a for cycle

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Mon Nov 26 21:06:55 CET 2007


You could try something like this (untested). 

lapply(unique(elio2$id), function(i){
  sub.mu <- exp.mu[exp.mu$id==i, ]
  n <- nrow(sub.mu)
  D <- matrix(0, n, n)
  diag(D) <- sub.mu$deriv.link
  A <- mat.cov[seq_len(n), seq_len(n)]
  R <- corstr[seq_len(n), seq_len(n)]
  SolveD <- solve(D)
  SolveA <- solve(sqrt(A))
  SolveD %*% SolveA %*% solve(R) %*% SolveA %*% SolveD
})

HTH,

Thierry

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research 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

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Niccolò Bassani
Verzonden: maandag 26 november 2007 16:36
Aan: r-help op stat.math.ethz.ch
Onderwerp: [R] Fwd: Empty list to use in a for cycle

Dear R-users,
I'm posting a problem I already asked help for some time ago, because I'm facing that problem once again and even because now, reading that old e-mail, and the answer recevied, I understand I've not made myself clear.

Here's the question: I need to create an empty list of a specific length to fill it with a quite large amount of square matrices, which is 602. The question is that these matrices are created inside a for cycle, and I do not know how to recall all of them one by one, except by creating an empty list before the cycle, than assigning for each value of the i index the amtrix computed to the first element of the empty list.
The fact is that: i've trided to create an empty list with

vector("list",602)

and then putting it in a cycle, but it didn't work. This is the cycle I've used. To prove it works (and then the cycle itself is not a problem) there's also the output (i.e. the last square matrix computed).

for (i in unique(elio2$id)){
sub.mu <- exp.mu[exp.mu$id==i,]
D <- matrix(0,nrow( sub.mu),nrow(sub.mu))
diag(D) <- sub.mu$deriv.link
A <- mat.cov[1:nrow(D),1:nrow(D)]
R <- corstr[1:nrow(D),1:nrow(D)]
W <- solve(D)%*%solve(sqrt(A))%*%solve(R)%*%solve(sqrt(A))%*%solve(D)
}

> W
              [,1]          [,2]          [,3]          [,4]
[1,]  3.492489e+02 -7.9324883721  0.0006286788 -0.0031046240 [2,] -7.932488e+00 17.4974625191 -1.7575467817  0.0001403319 [3,]  6.286788e-04 -1.7575467817 17.3227959738 -1.7529916860 [4,] -3.104624e-03  0.0001403319 -1.7529916860 17.2279244622
>

Does anyone knows how to insert each and every matrix like the one above in a "omnicomprehensive" list? That's because I've to use a function requiring me to have the matrices I need inside a list.
Thanks in advance, hope it's not a too much stupid problem!
niccolò

	[[alternative HTML version deleted]]



More information about the R-help mailing list