KABELI MEFANE
2009-Sep-09 23:06 UTC
[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 [[alternative HTML version deleted]]
Greg Hirson
2009-Sep-09 23:46 UTC
[R] Help on percentage of random numbers for different classes
Kabeli, Try sample (see ?sample) and set the prob argument to have larger probabilities for bigger numbers: set.seed(5) x = sample(1:10, 100, prob = c(.03, .03, .04, .05, .05, .05, .15, .2, .2, .2), replace=TRUE) >table(x) x 1 2 3 4 5 6 7 8 9 10 4 3 7 5 9 3 10 18 24 17 Does that get you what you need? Greg KABELI MEFANE wrote:> 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 > > > > [[alternative HTML version deleted]] > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Greg Hirson ghirson at ucdavis.edu Graduate Student Agricultural and Environmental Chemistry 1106 Robert Mondavi Institute North One Shields Avenue Davis, CA 95616
Nordlund, Dan (DSHS/RDA)
2009-Sep-10 00:17 UTC
[R] Help on percentage of random numbers for different classes
> -----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
Robert Baer
2009-Sep-10 07:46 UTC
[R] Help on percentage of random numbers for different classes
> 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 functionSomething like this might work for you: # set total sample size to greater than or equal to 100 # and mod 0 wrt 100 to preserve the percentages # here say 200 ss=200 # Create a vector with the desired properties vector=c( sample(2:7,ss/10,replace=TRUE), sample(3:7,ss/15,replace=TRUE), sample(3:9,ss/20,replace=TRUE), sample(3:10,ss/20,replace=TRUE), sample(5:10,ss/10,replace=TRUE)) # print out the vector vector> > > 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 > > > > [[alternative HTML version deleted]] > >> ______________________________________________ > R-help at r-project.org mailing list > 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. >