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.