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 numbers Individually 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]]
Try this:> result <- list() # holds the numbers for each column > for (i in seq(ncol(rate_number))){+ ans <- NULL + for (j in seq(nrow(rate_number))){ + ans <- c(ans, runif(rate_number[j, i], range_mat[j, i], range_mat[j + 1, i])) + } + result[[i]] <- ans + }> result[[1]] [1] 6.498850 6.608809 6.745953 6.440018 6.638723 7.217353 6.856071 7.075837 [9] 6.812778 6.883610 6.943057 6.756695 6.941194 7.184845 6.920174 6.991040 [17] 7.049783 6.996771 6.843109 7.163687 8.252700 8.441360 7.411915 8.335566 [25] 7.866912 8.481419 8.220590 8.424399 8.079554 8.044579 8.434034 7.284997 [33] 7.965845 8.348471 8.289097 7.966429 8.541814 7.907146 7.617196 7.356019 [41] 7.399199 7.724408 8.027951 8.243008 7.860245 8.619314 7.690405 7.938599 [49] 7.748592 8.226306 7.637025 7.967818 8.399466 7.376370 8.562982 7.758609 [57] 8.509161 7.770025 7.750662 7.964527 8.588298 8.546509 7.834984 8.415981 [65] 8.690927 7.901989 8.318772 7.849992 7.738028 8.385631 7.554038 8.316682 [73] 7.432538 7.618233 7.464957 7.609444 7.338402 8.213432 8.564404 8.418372 [81] 9.148654 8.977637 8.955042 9.155435 9.052467 9.077362 8.926599 8.885130 [89] 9.246342 9.066747 8.856604 8.814686 8.989059 9.212037 9.049380 9.738085 [97] 9.615896 9.428363 9.465737 9.324106 [[2]] [1] 9.006539 9.357783 9.051592 9.223142 9.320051 9.495919 9.247797 [8] 9.242175 9.086721 9.377410 9.226948 9.255585 9.103773 9.114329 [15] 9.297856 9.287436 9.038532 9.017770 9.321398 9.464308 10.098092 [22] 10.060901 10.026028 10.485095 10.007642 10.182788 10.101541 9.738869 [29] 9.758166 10.229310 9.952571 9.675127 10.246698 9.604988 10.364545 [36] 10.114645 10.057160 9.828777 9.953131 10.000441 9.680866 10.029631 [43] 9.575276 9.777756 9.712700 9.784790 10.395094 9.946235 10.279985 [50] 10.380619 9.913124 9.563808 9.835487 10.223726 9.837615 10.130414 [57] 10.340615 10.356132 9.891359 9.880494 10.395445 10.144316 10.241079 [64] 10.105303 10.403082 9.793730 9.691260 10.386451 10.003339 10.377058 [71] 9.689194 10.258103 10.224499 10.443725 10.047647 10.211744 9.888905 [78] 9.600873 10.427302 9.783233 10.795287 10.555180 10.920254 10.658982 [85] 10.891426 10.633754 10.609323 10.758398 10.634475 10.590584 10.759288 [92] 10.781391 10.564578 10.628184 10.858968 10.980705 10.550070 10.881611 [99] 10.973983 10.909317 [[3]] [1] 4.404146 4.574790 4.726678 4.726866 4.419990 4.381237 4.332727 4.411084 [9] 4.505063 4.711984 5.005480 4.878811 4.773230 4.958928 5.177001 4.923615 [17] 4.815721 4.937243 5.065710 4.945039 5.094814 5.094707 5.027450 4.964812 [25] 4.976360 4.903222 5.039177 5.205185 4.821302 4.957524 5.355463 5.464375 [33] 5.316345 5.480048 5.721479 5.630987 5.716455 5.485339 5.551794 5.492495 [41] 5.304403 5.373863 5.499257 5.436433 5.717346 5.511993 5.408572 5.388983 [49] 5.643770 5.601231 5.332514 5.282229 5.627353 5.560205 5.334788 5.281107 [57] 5.304515 5.440858 5.334655 5.399326 5.346105 5.378585 5.340616 5.488657 [65] 5.635369 5.263894 5.513655 5.690160 5.436532 5.273980 5.819314 5.910746 [73] 5.827416 5.816114 5.860653 5.863190 5.815708 6.240782 5.913507 6.003470 [81] 6.090721 5.799585 5.809451 5.775220 6.214627 6.086856 5.797429 5.996298 [89] 5.980776 5.937608 6.745550 6.338175 6.656718 6.284223 6.450225 6.320572 [97] 6.346655 6.670676 6.609957 6.383606 On Fri, Sep 3, 2010 at 6:32 AM, Sarah Sanchez <sarah_sanchez09 at yahoo.com> 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 numbers?Individually 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 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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Sep 3, 2010, at 6:32 AM, 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).This should do it for the first column and modifying to for additional columns should be straightforward: > mapply(runif, rate_number[,1], range_mat[1:5,1], range_mat[2:6,1]) #--------- rlist <- list() for (i in 1:ncol(rate_number) ) rlist[[i]] <-mapply(runif, rate_number[,i], range_mat[1:5,i], range_mat[2:6,i]) > str(rlist) List of 3 $ :List of 5 ..$ : num [1:5] 6.38 6.66 6.65 6.27 6.69 ..$ : num [1:15] 6.81 7.05 6.98 7.03 7.12 ... ..$ : num [1:60] 7.34 8.4 7.95 8.53 8.12 ... ..$ : num [1:15] 9.25 8.91 8.91 8.77 9.01 ... ..$ : num [1:5] 9.58 9.49 9.34 9.61 9.29 $ :List of 5 ..$ : num(0) ..$ : num [1:20] 9.23 9.47 9.42 9.36 9.06 ... ..$ : num [1:60] 10.06 9.81 9.58 10.28 9.69 ... ..$ : num [1:20] 10.9 10.8 10.6 10.5 11 ... ..$ : num(0) $ :List of 5 ..$ : num [1:10] 4.36 4.58 4.56 4.27 4.72 ... ..$ : num [1:20] 5.15 5.09 4.99 4.98 5.06 ... ..$ : num [1:40] 5.71 5.35 5.26 5.41 5.58 ... ..$ : num [1:20] 5.87 5.93 5.87 6.22 5.96 ... ..$ : num [1:10] 6.38 6.62 6.74 6.53 6.45 ...> > 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 numbers Individually 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 >David Winsemius, MD West Hartford, CT
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 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Hi: Here's a slightly different approach, using both mapply() in base (a la David) and mlply() in plyr. My thought was to put the arguments together into a three column data frame; the names were chosen to correspond to the first three arguments of runif(): params <- data.frame(n = as.vector(rate_number), min = as.vector(range_mat[-6, ]), max = as.vector(range_mat[-1, ]))> paramsn min max [1,] 5 6.25 6.75 [2,] 15 6.75 7.25 [3,] 60 7.25 8.75 [4,] 15 8.75 9.25 [5,] 5 9.25 9.75 [6,] 0 8.50 9.00 [7,] 20 9.00 9.50 [8,] 60 9.50 10.50 [9,] 20 10.50 11.00 [10,] 0 11.00 11.50 [11,] 10 4.25 4.75 [12,] 20 4.75 5.25 [13,] 40 5.25 5.75 [14,] 20 5.75 6.25 [15,] 10 6.25 6.75 Since the sample sizes are to be unequal, we need to output them to a list, which can be collapsed at the end if a vector is preferred. # mapply() approach: ll <- list() ll <- with(params, mapply(runif, n, min, max))> sapply(ll, length)[1] 5 15 60 15 5 0 20 60 20 0 10 20 40 20 10> sum(sapply(ll, length) )[1] 300 myUniformSample1 <- unlist(ll) # collapse to vector # mlply approach: the input data frame is the first argument, followed by the function name library(plyr) samp <- mlply(params, runif) sapply(samp, length) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 5 15 60 15 5 0 20 60 20 0 10 20 40 20 10> sum(sapply(smp, length))[1] 300 myUniformSample2 <- unlist(samp) HTH, Dennis On Fri, Sep 3, 2010 at 3:32 AM, Sarah Sanchez <sarah_sanchez09@yahoo.com>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 numbers Individually 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. > >[[alternative HTML version deleted]]