Re: [R] Randomly sampling subsets of dataframe variable

2010-03-12 Thread Ali Tofigh
I would just get row indices: row.indices <- as.vector(sapply(0:25 * 5 + 1, function(x) {sort(sample(x:(x+4), 2))})) new.data.fram <- your.data.frame[row.indices, ] Cheers, /Ali On Fri, Mar 12, 2010 at 15:06, Hosack, Michael wrote: > Fellow R users, > > I am stumped on what would seem to be som

Re: [R] Randomly sampling subsets of dataframe variable

2010-03-12 Thread Chuck Cleland
On 3/12/2010 3:06 PM, Hosack, Michael wrote: > Fellow R users, > > I am stumped on what would seem to be something fairly simple. > I have a dataframe that has a variable named 'WEEK' that takes > the numbers 1:26 (26 week time-period) with each number repeated > five times consecutively (once

Re: [R] Randomly sampling subsets of dataframe variable

2010-03-12 Thread Dennis Murphy
Hi: A ddply solution: library(plyr) somedata = data.frame(week=rep(1:26,rep(5,26)),day=rep(1:5,26)) # sample two rows out of five per week daysamp <- function(x) x[sample(1:5, 2), ] # Ram it through ddply: ddply(somedata, .(week), daysamp) First part of output: week day 1 1 4 2 1

Re: [R] Randomly sampling subsets of dataframe variable

2010-03-12 Thread Stephan Kolassa
Hi Mike, take an index vector that selects Monday and Tuesday out of each week, and then run a restricted random permutation on this vector which only permutes indices within each week. rperm() is in the sna package. library(sna) foo <- rep(c(TRUE,TRUE,FALSE,FALSE,FALSE),26) your.data[foo[rpe

Re: [R] Randomly sampling subsets of dataframe variable

2010-03-12 Thread David Winsemius
On Mar 12, 2010, at 3:06 PM, Hosack, Michael wrote: Fellow R users, I am stumped on what would seem to be something fairly simple. I have a dataframe that has a variable named 'WEEK' that takes the numbers 1:26 (26 week time-period) with each number repeated five times consecutively (once for

Re: [R] Randomly sampling subsets of dataframe variable

2010-03-12 Thread Phil Spector
Mike - Perhaps these suggestions will be helpful: somedata = data.frame(week=rep(1:26,rep(5,26)),day=rep(1:5,26)) res = by(somedata,somedata$week,function(x)x[sample(1:nrow(x),2),]) do.call(rbind,res) or do.call(rbind,lapply(split(somedata,somedata$week), function(x)x[sample(