Sarah, Here's a version using two nested loops:
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) set.seed(123) res <- vector(mode = "list", length = NCOL(rate_number)) rateSum <- colSums(rate_number) for(i in seq_len(NCOL(rate_number))) { res[[i]] <- numeric(length = rateSum[i]) rates <- rate_number[,i] rates <- rates[(want <- which(rates > 0))] for(j in seq_along(rates)) { if(j == 1) { from <- 1 to <- rates[1] } else { from <- sum(rates[1:(j-1)]) + 1 to <- sum(rates[1:j]) } res[[i]][from:to] <- runif(rates[j], min = range_mat[want[j],i], max = range_mat[want[j]+1, i]) } } str(res) head(res) Not the most elegant code but it works... Most of the fiddling about in there is to work round the fact that you have 0's in some cells of rate_number and I wanted to avoid concatenating vectors to enlarge them, so allocate storage first. HTH G On Fri, 2010-09-03 at 03:32 -0700, 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). > > 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 numbersIndividually 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 > > Sarah > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@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.