[R] A coding question

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri Jun 2 10:37:58 CEST 2006


xpRt.wannabe wrote:

> Dear List:
> 
> I have the follow code:
> 
> y <- replicate(10,replicate(8,sum(rnorm(rpois(1,5)))))
> 
> Now I need to apply the following condition to _every_ randomly generated
> Normal number in the code above:
> 
> x - max(0,x-15) + max(0,x-90), where x represents the individual Normal
> numbers.
> 
> In other words, the said condition needs to be applied before
> replicate(...(replicate(...(sum(...))) takes place.
> 
> Any help would be greatly appreciated.


y <- replicate(10, {
         rp <- rpois(8, 5)
         mysum <- sapply(rp, function(x) {
             x <- rnorm(x)
             x <- x - max(0, x-15) + max(0, x-90)
             sum(x)
         })
      })

> 
> platform i386-pc-mingw32
> arch i386
> os mingw32
> system i386, mingw32
> status
> major 2
> minor 2.1
> year 2005
> month 12
> day 20
> svn rev 36812
> language R
> 
> ______________________________________________
> 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