[R] Generation of uniform random numbers
David Winsemius
dwinsemius at comcast.net
Fri Sep 3 16:10:01 CEST 2010
On Sep 3, 2010, at 6:32 AM, Sarah Sanchez wrote:
>
>
>
>
> 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).
This should do it for the first column and modifying to for additional
columns should be straightforward:
> mapply(runif, rate_number[,1], range_mat[1:5,1], range_mat[2:6,1])
#---------
rlist <- list()
for (i in 1:ncol(rate_number) ) rlist[[i]] <-mapply(runif,
rate_number[,i], range_mat[1:5,i], range_mat[2:6,i])
> str(rlist)
List of 3
$ :List of 5
..$ : num [1:5] 6.38 6.66 6.65 6.27 6.69
..$ : num [1:15] 6.81 7.05 6.98 7.03 7.12 ...
..$ : num [1:60] 7.34 8.4 7.95 8.53 8.12 ...
..$ : num [1:15] 9.25 8.91 8.91 8.77 9.01 ...
..$ : num [1:5] 9.58 9.49 9.34 9.61 9.29
$ :List of 5
..$ : num(0)
..$ : num [1:20] 9.23 9.47 9.42 9.36 9.06 ...
..$ : num [1:60] 10.06 9.81 9.58 10.28 9.69 ...
..$ : num [1:20] 10.9 10.8 10.6 10.5 11 ...
..$ : num(0)
$ :List of 5
..$ : num [1:10] 4.36 4.58 4.56 4.27 4.72 ...
..$ : num [1:20] 5.15 5.09 4.99 4.98 5.06 ...
..$ : num [1:40] 5.71 5.35 5.26 5.41 5.58 ...
..$ : num [1:20] 5.87 5.93 5.87 6.22 5.96 ...
..$ : num [1:10] 6.38 6.62 6.74 6.53 6.45 ...
>
> 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
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list