3

Following some excellent replies to an earlier question I posed - selecting n random rows across all levels of a factor within a dataframe - I have been considering an extension to this problem.

The previous question sought to randomly sample n rows/observations from each level of a particular factor, and to combine all information in a new dataframe.

However, this sort of random sampling may not be optimal for some types of data. Here, I want to again select n rows/observations per every level of a particular factor. The major difference here is that the rows/observations selected from each level of the particular factor should be consecutive.

This is an example dataset:

id<-sample(1:20, 100, replace = TRUE)
dat<-as.data.frame(id)
color <-  c("blue", "red", "yellow", "pink", "green", "orange", "white", "brown")
dat$colors<- sample(color, 100, replace = TRUE)

To add to this example dataset are timestamps for each observation. These will form the order along which I wish to sample. I am using a function suggested in this thread - efficiently generate a random sample of times and dates between two dates - for this purpose:

randomts <- function(N, st="2013/12/09", et="2013/12/14") {
st <- as.POSIXct(as.Date(st))
et <- as.POSIXct(as.Date(et))
dt <- as.numeric(difftime(et,st,unit="sec"))
ev <- sort(runif(N, 0, dt))
rt <- st + ev
}

dat$ts<-randomts(100)

I am not sure if this is necessary, but it is also possible to add a variable that gives the 'day'. This is the factor which I wish to sample from every level.

temp<-strsplit(as.character(dat$ts), " ")
mat<-matrix(unlist(temp), ncol=2, byrow=TRUE)
df<-as.data.frame(mat)
colnames(df)<-c("date", "time")
dat<-cbind(df, dat)

mindate<-as.Date(min(dat$date))
dates<-as.Date(dat$date)
x<-as.numeric(dates-mindate)
x<-x+1 
dat$day<-x  
as.factor(dat$day) #in this example data there are 6 levels to 'day'.

#EDIT there may be 5 levels to day - depends on how data randomly generated by function 

Original post did not accurately calculate day. This is better though not perfect. Seems ok but first day is day=0, when would like it to be day=1

To summarize, the problem is this. I want to create a new dataframe that contains e.g. 5 consecutive observations randomly sampled from every level of the factor day of the dataframe "dat" (ie 5 random consecutive observations taken from every day). Therefore, the new dataframe would have 30 observations. An additional caveat would be that if I wanted to sample e.g. 20 consecutive observations, and a particular level only had 15 observations, then all 15 are returned and there is no replacement.

I have tried to play around with seq_along to solve this. I seem to be able to get this to work for one variable at a time - e.g. if sampling from colors:

x <-  sample(seq_along(dat$colors),1)
dat$colors[x:(x+4)]

This produces a randomly sampled list of 5 consecutive colors from the variable colors.

I am having trouble applying this to the problem at hand. I have tried modifying some of the answers to my previous question selecting n random rows across all levels of a factor within a dataframe - but can't seem to work out the correct placement of seq_along in any.

Community
  • 1
  • 1
jalapic
  • 13,792
  • 8
  • 57
  • 87
  • Hi, Take a bit of time and read the tag excerpt before tagging. [tag:dataframes] is for pandas, whereas you need [tag:data.frame] here. Be careful the next time. See this meta post. [Warn \[r\] users from adding \[dataframes\] tag instead of \[data.frame\] tag](http://meta.stackoverflow.com/q/318933) – Bhargav Rao Mar 14 '16 at 14:49

1 Answers1

1

This should sample runs of colors assuming your data.frame is sorted by date. Here N is how many of each color you want. The return value keep will be TRUE for the runs for each color group.

N <- 5
keep <- with(dat, ave(rep(T, nrow(dat)), colors, FUN=function(x) {
    start <- sample.int(max(length(x)-N,1),1)
    end <- min(length(x), start+N-1)
    r <- rep(c(F,T,F), c(start-1, end-start+1, length(x)-end)) 
}))
dat[keep, ]

This method does not look at any day value. It simply find a random run of N observations. It will only return fewer per category if there are fewer than N observations for a particular group.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks. I haven't tested this across a range of data types, but it seems to be working. To make the factor level 'day' as per the original question, I changed 'colors' to 'day' in your code – jalapic May 24 '14 at 16:57