vendredi 11 janvier 2019

Using a loop with sample() based on conditions in dataframe

I have a dataset with dates generated from this code:

library(chron)
dates <- seq.dates("1/1/2019", "1/6/2020", by = "days")
week <- c(1:53)
day <- c("tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", 
"monday")
weeks <- rep(1:53, each = 7)
dates_df <- data.frame(date=dates, day=day, week=weeks)

And I'm trying to use a loop with sample() to randomly select 3 days from each week without replacement but I can't seem to find a way to indicate in my loop that I need to sample 3 times for every integer in dates_df$week then move to the next week.

I can't simply sample on a set sequence (7 days for instance) because on an actual calendar some months end in the middle of the week.

Is there a way to use sample() conditionally based on other values in a dataframe?




Aucun commentaire:

Enregistrer un commentaire