From: Constantine Tsardounis <costas.magnuse_at_gmail.com>

Date: Tue 31 Jan 2006 - 04:41:11 EST

...

[[50]]

}

R-help@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 Received on Tue Jan 31 04:52:08 2006

Date: Tue 31 Jan 2006 - 04:41:11 EST

Hello, good morning or evening!...

[[1]] [1]........................[1000] [[2]] [1]........................[840]

...

[[50]]

[1]........................[945]

Having a script with loops inside loops (for example in a Monte-Carlo simulation) takes a lot of minutes before it is completed. Is there another easier way to perform functions for each of the [[i]] ? Using probably apply? or constructing a specific function? or using the so-called "vectorising" tricks?

One example could be the following, that calculates the sums 1:5, 2:6, 3:7,..., for each of xs[[i]] :

xs <- lapply(1:500, function(x) rnorm(1000))
totalsum <- list()

sums <- list()

first <- list()

for(i in 1:length(xs)) {

totalsum[i] <- sum(xs[[i]])

for(j in 1:length(xs[[i]])) { if(j == 1) { sums[[i]] <- list() } if(j >= 5) { sums[[i]][j] <- sum(xs[[i]][(j-4):j]) } }

}

example.list[[i]] <- expression

demands more than 50 lines at the beginning of the script that
"initiate" the lists (e.g.

example.list.1 <- list()

example.list.2 <- list()

...

example.list.50 <- list()

<< 2 >>. Is there a way to avoid that?

Thank you very very much in advance,

Constantine Tsardounis

R-help@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 Received on Tue Jan 31 04:52:08 2006

*
This archive was generated by hypermail 2.1.8
: Fri 03 Mar 2006 - 03:42:15 EST
*