Hi all,

I have some rough code to sample consecutive integers with length according to a vector of lengths

#sample space (representing positions)
pos<-c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)

#sample lengths
lengths<-c(2,3,2)

From these two vectors I need a vector of sampled positions.


the sampling is without replacement, making things tough as the sampled integers need to be consecutive. Im hoping somebody knows a faster way of doing it than I have. ATM its way to slow on large vectors.


samplePos<-function(l){
   start.pos<-sample(pos,1)
   end.pos<-start.pos+l-1
   posies<-start.pos:end.pos
   posies
}

s.start<-c()


newPos<-function(a){
   rp<-samplePos(a)
   #test sampled range is consecutive, if not resample
   if (length(rp) != rp[a]+1 -rp[1]){rp<-samplePos(a)}
   pos<-setdiff(pos,rp)
   rp[1]
}

newps<-c()
newps<-unlist(lapply(lengths,newPos))

I think the bottleneck may be on the setdiff() function - the sample space is quite large so I dont think there would be too many rejections.



Many thanks,
Chris

______________________________________________
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.

Reply via email to