[R] R: LIST function and LOOPS

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Mar 10 16:29:34 CET 2005


You will need to capture the value of ss at the end of each 'i' as such

z4 <-function(w){

  output <- numeric(w)
  
  for (i in 1:w){
    
    set.seed(i+6)  # this is redundant line
    ss<-0
    
    for (j in 1:5){
      set.seed(j+1+(i-1)*6)
      r<-rnorm(1)
      ss<-ss+r
    }

    output[i] <- ss
  }
  return(output)
}

BTW, I do not think it is a good idea to set.seed() so many times.


To answer you more general question, see if the following is useful.
I am trying to simulate 'n' values from a standard normal distribution
but 'n' is random variable itself.

f <-function(w, lambda=3){
 
  tmp <- list(NULL)
  
  for (i in 1:w){
    n <- 1 + rpois(1, lambda=lambda)  # number of simulation required
    tmp[[ i ]]  <- rnorm(n)
  }

  # flatten the list into a ragged matrix
  out.lengths   <- sapply(tmp, length)
  out           <- matrix( nr=w, nc=max( out.lengths ) )
  rownames(out) <- paste("w =", 1:w)
  for(i in 1:w) out[i, 1:out.lengths[i] ] <- tmp[[i]]

  return(out)
}

f(6, lambda=3)

It is not very elegant but I hope that helps you out somehow.

Regards, Adai



On Thu, 2005-03-10 at 10:16 +0200, Clark Allan wrote:
> hi all
> 
> another simple question.
> 
> i've written a dummy program so that you get the concept. (the code
> could be simplfied such that there are no loops. but lets leave the
> loops in for now.)
> 
> z1<-function(w)
> {
> for (i in 1:w)
> {
> set.seed(i+6)
> ss<-0
> 	for (j in 1:5)
> 	{
> 		set.seed(j+1+(i-1)*6)
> 		r<-rnorm(1)
> 		ss<-ss+r
> 	}
> list(ss=ss)
> }
> }
> check.1<-z1(3)
> check.1
> 
> the results is:
> $ss
> [1] -0.01516304
> 
> 
> what i want is something that looks like this:
> 
> j=1
> $ss
> [1] -2.213343
> 
> j=2
> $ss
> [1] -2.904235
> 
> j=3
> $ss
> [1] -0.01516304
> 
> 
> i know that i could use the print command. (see z2)
> 
> z2<-function(w)
> {
> for (i in 1:w)
> {
> set.seed(i+6)
> ss<-0
> 	for (j in 1:5)
> 	{
> 		set.seed(j+1+(i-1)*6)
> 		r<-rnorm(1)
> 		ss<-ss+r
> 	}
> print(ss)
> }
> }
> check.2<-z2(3)
> check.2
> 
> > check.2<-z2(3)
> [1] -2.213343
> [1] -2.904235
> [1] -0.01516304
> > check.2
> [1] -0.01516304
> 
> the problem with z2 is that only the last value is saved.
> 
> 
> what i could do is use matrices like the following: (but i dont want to
> do this AND WOULD PREFER TO USE list.)
> 
> z3<-function(w)
> {
> results.<-matrix(nrow=w,ncol=1)
> colnames(results.)<-c("ss")
> for (i in 1:w)
> {
> set.seed(i+6)
> ss<-0
> 	for (j in 1:5)
> 	{
> 		set.seed(j+1+(i-1)*6)
> 		r<-rnorm(1)
> 		ss<-ss+r
> 	}
> results.[i,1]<-ss
> }
> results.
> }
> check.3<-z3(3)
> check.3
> 
> > check.3
>               ss
> [1,] -2.21334260
> [2,] -2.90423463
> [3,] -0.01516304
> 
> what if i have a new program (something different) and i want the
> following:
> 
> j=1
> $a
> 1
> 2
> 3
> 
> $b
> 1
> 2
> 3
> 4
> 5
> 
> $c
> 1
> 
> 
> ###############
> j=2
> $a
> 11
> 21
> 31
> 
> $b
> 11
> 21
> 31
> 41
> 51
> 
> $c
> 11
> 
> ###############
> j=3
> $a
> 21
> 22
> 32
> 
> $b
> 21
> 22
> 32
> 42
> 52
> 
> $c
> 21
> 
> MATRICES SEEMS TO BE A GOOD WAY OF DOING THIS (but then you would have
> to set up three matrices, one for a,b and c). BUT WHAT IF I WANT TO USE
> THE LIST FUNCTION? i.e. there is a list in the first loop that i want to
> display!
> 
> sorry for the long mail.
> 
> ***
> ALLAN
> ______________________________________________ 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




More information about the R-help mailing list