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

2015-12-15 Thread Frank S.
Many thanks to David L Carlson, Ben Gunter and David Winsemius for your quick and very elegant solutions!! With your list answers I am learning sa lot of things that will help me in the future to program. Best, Frank S. > Subject: Re: [R] Random selection of a fixed number of values

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

2015-12-14 Thread David Winsemius
> On Dec 14, 2015, at 12:46 PM, David L Carlson wrote: > > There are lots of ways to do this. For example, Another method with mapply: mapply(function( n, vals) {sample(vals$id, n)} , # no replacement is the default for sample vals= split(data, findInterval(data$value, 0:5) )[1

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

2015-12-14 Thread Bert Gunter
Yes. May I suggest: grp <- c("[0,1)", "[1,2)", "[2,3)", "[3,4)", "[4,5)") can be obtained more simply as grp <- levels(groups)[1:5] and one slight aesthetic change in the indexing: from: samples <- lapply(1:5, function(x) sample(data$id[groups==grp[x]], size[x])) to: samples <- lapply(1:5, f

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

2015-12-14 Thread David L Carlson
There are lots of ways to do this. For example, > groups <- cut(data$value, include.lowest = T, right = FALSE, + breaks = 0:ceiling(max(data$value))) > grp <- c("[0,1)", "[1,2)", "[2,3)", "[3,4)", "[4,5)") > size <- c(10, 7, 5, 5, 3) > set.seed(42) > samples <- lapply(1:5, function(x) sample(