[R] Help on percentage of random numbers for different classes
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Thu Sep 10 02:17:13 CEST 2009
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of KABELI MEFANE
> Sent: Wednesday, September 09, 2009 4:06 PM
> To: R-help at r-project.org
> Subject: Re: [R] Help on percentage of random numbers for different classes
>
> R-list
>
> I am sorry for asking this stupid question, but i have been running in circles. I want
> to randomly generate a scaling point of between 1 and 10, for say hundred entries,
> where the first 10% percent is has rates between 2 and 7, the next 15% 3 and 7,
> 20% between 3 and 9, 20% between 3 and 10, 35% between 5 and 10. The
> problem is that i can only generate the usual 100 using runif function
>
> > y<-c(ceiling(10*runif(100)))
> > y
> [1] 10 8 5 2 4 1 6 7 1 6 8 8 8 9 7 7 8 8 2 7 3 10 1 7 1
> [26] 10 4 8 8 8 9 3 7 8 4 6 7 2 3 1 9 8 2 6 7 4 8 8 9 7
> [51] 6 5 4 1 8 7 9 8 10 5 3 7 5 5 4 4 7 4 10 4 9 1 5 10 10
> [76] 5 5 10 7 3 4 4 9 10 6 2 6 6 6 3 8 2 2 4 4 10 6 9 4 3
>
> I just want to try to avoid small numbers as much as possible. I am open to
> suggestions, please please please.
>
> Kabeli
>
>
If I understand you correctly, this might do what you want. n is a vector with the number samples you want in each range, x is then minimum for each range and y is the maximum. The function, s, samples from a given range, a specified number of times. mapply applies the function using the first, second, ... elements in turn returning a list with the samples.
n <- c(10,15,20,20,35)
x <- c(2,3,3,3,5)
y <- c(7,7,9,10,10)
s <- function(mn, mx, n) {sample(mn:mx, n, replace=TRUE)}
unlist(mapply(s, x, y, n))
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list