[R] Random selection of a fixed number of values by interval

Frank S. f_j_rod at hotmail.com
Mon Dec 14 21:01:45 CET 2015


Dear R users,
 
I'm writing to this list because I must get a random sample (without replacement) from a given vector, but the clue is that I need to extract a fixed number of values by each prespecified 1-unit interval. As an example I try to say, I have a data frame that looks like this (my real dataframe is bigger):
 
data <- data.frame(id = 1:70, value=  c(0.68, 2.96, 1.93, 5.63, 3.08, 3.10, 2.99, 1.79, 2.96, 0.85, 11.79, 0.06, 4.31, 0.64, 1.43, 0.88, 2.79, 4.67,
      1.23, 1.43, 3.05, 2.44, 2.55, 3.82, 3.55, 1.56, 7.25, 2.75, 9.64, 5.14, 3.54, 3.12, 0.17, 1.07, 4.08, 4.47, 5.58, 7.41, 0.85, 4.30, 7.58,
      0.58, 1.40, 4.74, 5.04, 0.14, 1.14, 3.28, 7.84, 0.07, 3.97, 1.02, 3.47, 0.66, 2.38, 0.06, 0.67, 0.48, 4.48, 0.12, 3.82, 2.27, 0.93, 0.30, 
      0.73, 0.33, 2.91, 0.81, 0.18, 0.42))
 
And I would like to select, in a random manner:
 
10 id's whose value belongs to [0,1) interval
7 id's whose value belongs to [1,2)
5 id's whose value belongs to [2,3)
5 id's whose value belongs to [3,4)
3 id's whose value belongs to [4,5)
 
# I have the following values by each 1-unit interval:
table(cut(data$value, include.lowest = T, right = FALSE, breaks = 0:ceiling(max(data$value))))
 
and the size vector:
size <- c(10, 7, 5, 5, 3) 
 
But I'm not able to get it by using sample function. Does anyone have some idea?
 
Thank you very much for any suggestions!!
 
Frank S.

 
 
 
 		 	   		  
	[[alternative HTML version deleted]]



More information about the R-help mailing list