Question

I am trying to generate appointment times for yearly scheduled visits. The available days=1:365 and the first appointment should be randomly chosen first=sample(days,1,replace=F)

Now given the first appointment I want to generate 3 more appointment in the space between 1:365 so that there will be exactly 4 appointments in the 1:365 space, and as equally spaced between them as possible.

I have tried

point<-sort(c(first-1:5*364/4,first+1:5*364/4 ));point<-point[point>0 & point<365]

but it does not always give me 4 appointments. I have eventually run this many times and picked only the samples with 4 appointments, but I wanted to ask if there is a more elegant way to get exactly 4 points as equally distanced a s possible.

Was it helpful?

Solution

I was thinking of equal spacing (around 91 days between appointments) in a year starting at the first appointment... Essentially one appointment per quarter of the year.

# Find how many days in a quarter of the year
quarter = floor(365/4)
first = sample(days, 1)
all = c(first, first + (1:3)*quarter)
all[all > 365] = all[all > 365] - 365
all
sort(all)

OTHER TIPS

Is this what you're looking for?

set.seed(1)    # for reproducible  example ONLY - you need to take this out.
first <- sample(1:365,1)
points <- c(first+(0:3)*(365-first)/4)
points
# [1]  97 164 231 298

Another way uses

points <- c(first+(0:3)*(365-first)/3)

This creates 4 points euqally spaced on [first, 365], but the last point will always be 365.

The reason your code is giving unexpected results is because you use first-1:5*364/4. This creates points prior to first, some of which can be < 0. Then you exclude those with points[points>0...].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top