Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:14:18+00:00 2026-06-15T15:14:18+00:00

I would like to sample values, but have a constraint in place that demands

  • 0

I would like to sample values, but have a constraint in place that demands two values are at least window apart. This would be akin to sampling days in a year, but setting the window to be at least a fortnight apart. So far I’ve tried it like this

check.diff <- TRUE
window <- 14
while (check.diff == TRUE) {
    sampled.session <- sort(sample(1:365, size = 5, replace = FALSE))
    check.diff <- any(diff(sampled.session) < window)
}

This works nicely if the window constraint is small. If one specifies a rather large value, this can become an infinite loop. While I can insert all sorts of checks and maximum number of iterations, I was wondering if there’s a smarter way of attacking this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:14:19+00:00Added an answer on June 15, 2026 at 3:14 pm

    One way to do this is by removing candidates from the population from which you take the sample:

    set.seed(42)
    
    population <- 1:356
    n_samples <- 5
    window <- 14
    
    sampled_session <- rep(sample(population, 1), n_samples) # initialize the vector
    
    for (i in seq.int(2, n_samples)) {
        borders <- sampled_session[i - 1] + (window - 1) * c(-1, 1)
        days_in_window <- seq.int(borders[1], borders[2])
        population <- setdiff(population, days_in_window)
        sampled_session[i] <- sample(population, 1) 
    }
    
    sort(sampled_session)
    # [1]  90 193 264 309 326
    
    diff(sort(sampled_session))
    # [1] 103  71  45  17
    

    Another way would be

    set.seed(357)
    population <- 1:357
    n_samples <- 5
    window <- 14
    
    sampled.session <- numeric(n_samples) 
    for (i in seq_len(n_samples)) {
        sampled.session[i] <- pick <- sample(population, 1)
        population <- population[-which(population < pick + window & population > pick - window)]
    }
    sort(sampled.session)
    [1]  19  39 111 134 267
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sample string where I would like to replace the star with
I have two set of samples that are time independent. I would like to
I would like to extract items from this sample html, more specificly, i would
I have a sample dataform and I would like to remove the ability to
i would like a simple help... i have a url like this: example.com/profile.php?id= &
Using Guava, I would like to have a map that has the following properties:
I have a mysql code like this... INSERT INTO `product` (`product_id`, `model`, `sku`) VALUES
I have a file which I would like to read data from. This is
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
I would like to to sample values from a vector s<-0:1440 to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.