[R] Help on percentage of random numbers for different classes

Robert Baer rbaer at atsu.edu
Thu Sep 10 09:46:42 CEST 2009


> 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

Something 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.
>




More information about the R-help mailing list