Java Mailing List Archive

http://www.r-help.com/

Home » R Help for Statistical Computing »

Re: [R] Generation of uniform random numbers

Dennis Murphy

2010-09-04

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi:

Here's a slightly different approach, using both mapply() in base (a la
David) and mlply() in plyr.

My thought was to put the arguments together into a three column data frame;
the names were chosen to correspond to the first three arguments of runif():
params <- data.frame(n = as.vector(rate_number),
                        min = as.vector(range_mat[-6, ]),
                        max = as.vector(range_mat[-1, ]))

> params
      n  min  max
[1,]   5  6.25  6.75
[2,]   15  6.75  7.25
[3,]   60  7.25  8.75
[4,]   15  8.75  9.25
[5,]   5  9.25  9.75
[6,]   0  8.50  9.00
[7,]   20  9.00  9.50
[8,]   60  9.50 10.50
[9,]   20 10.50 11.00
[10,]   0 11.00 11.50
[11,]   10  4.25  4.75
[12,]   20  4.75  5.25
[13,]   40  5.25  5.75
[14,]   20  5.75  6.25
[15,]   10  6.25  6.75

Since the sample sizes are to be unequal, we need to output them to a list,
which can be collapsed at the end if a vector is preferred.

# mapply() approach:
ll <- list()
ll <- with(params, mapply(runif, n, min, max))
> sapply(ll, length)
[1] 5 15 60 15 5 0 20 60 20 0 10 20 40 20 10
> sum(sapply(ll, length) )
[1] 300
myUniformSample1 <- unlist(ll)   # collapse to vector

# mlply approach: the input data frame is the first argument, followed by
the function name

library(plyr)
samp <- mlply(params, runif)
sapply(samp, length)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
5 15 60 15 5 0 20 60 20 0 10 20 40 20 10
> sum(sapply(smp, length))
[1] 300
myUniformSample2 <- unlist(samp)

HTH,
Dennis


On Fri, Sep 3, 2010 at 3:32 AM, Sarah Sanchez <sarah_sanchez09@(protected):

>
>
>
>
> Dear R helpers
>
> I have following dataset
>
> rate_number = matrix(c(5, 15, 60, 15, 5, 0, 20, 60, 20,0, 10, 20, 40, 20,
> 10), nrow = 5, ncol = 3)
>
> range_mat = matrix(c(6.25, 6.75, 7.25, 8.75, 9.25, 9.75, 8.5, 9, 9.5, 10.5,
> 11, 11.5, 4.25, 4.75, 5.25, 5.75, 6.25, 6.75), nrow = 6, ncol = 3)
>
> > rate_number
>     [,1]  [,2]  [,3]
> [1,]   5   0    10
> [2,]  15  20    20
> [3,]  60  60    40
> [4,]  15  20    20
> [5,]   5   0    10
>
> > range_mat
>    [,1]    [,2]   [,3]
> [1,] 6.25   8.5  4.25
> [2,] 6.75   9.0  4.75
> [3,] 7.25   9.5  5.25
> [4,] 8.75  10.5  5.75
> [5,] 9.25  11.0  6.25
> [6,] 9.75  11.5 6.75
>
> My problem is to generate random numbers in line with rate_number and using
> the range_mat. E.g.
>
> I need to generate (5, 15, 60, 15, 5 i.e. the first column of rate_number)
> uniform random numbers (using 1st column of range_mat) s.t the first 5
> numbers will be in the range (6.25 - 6.75), next 15 numbers should be in the
> range (6.75 to 7.25), next 60 numbers should be in the range (7.25 to 8.75),
> next 15 numbers in the range (8.75 to 9.25) and last 5 numbers in the range
> (9.25 to 9.75).
>
> Similarily, I need to generate (0, 20, 60, 20, 0 i.e. 2nd column of
> rate_number) uniform random numbers in the range (using 2nd column of
> range_mat) i.e. (8.5 to 9), (9 to 9.5), (9.5 to 10.5), (10.5 to 11), (11 to
> 11.5) respectively.
>
> I could have generated these random numbers Individually using runif, but
> main problem is range_number could be anything i.e. there may be 50 rates
> but for each rate, no of rate combination will always be 5 i.e. rate_number
> will always have 5 rows only and also range_mat will always have 6 rows
> only.
>
> I tried writing loops and even tapply etc. but just can't get through.
>
> I sincerely request you to kindly guide me.
>
> Regards
>
> Sarah
>
>
>
>
>
>
>
>     [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@(protected)
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

 [[alternative HTML version deleted]]

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
©2008 r-help.com - Jax Systems, LLC, U.S.A.