[R] A coding question
xpRt.wannabe
xprt.wannabe at gmail.com
Sat Jun 3 04:17:59 CEST 2006
Uwe and Ben,
Thank you both for your help.
To me, both sets of code seem to do the job and should produce the
same results. However, as a test I inserted set.seed( ) as follows.
Unless I put set.seed( ) in the wrong lines, the results produced by
both sets of code turn out to be different. I am baffled.
y <- replicate(10, {
set.seed(123)
rp <- rpois(8, 5)
mysum <- sapply(rp, function(x) {
set.seed(123)
x <- rnorm(x)
x <- x - max(0, x-15) + max(0, x-90)
sum(x)
})
})
##
tmpf <- function() {
set.seed(123)
x <- rnorm(rpois(1,5))
sum(ifelse(x>90,x-75,pmin(x,15)))
}
replicate(10,replicate(8,tmpf()))
On 6/2/06, Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
> 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)
> })
> })
More information about the R-help
mailing list