Get the range and random days within that range using R
问题 I have a data frame like as shown below test_df <- data.frame("subbject_id" = c(1,2,3,4,5), "date_1" = c("01/01/2003","12/31/2007","12/30/2008","01/02/2007","01/01/2007")) test_df = test_df %>% mutate(date_1 = mdy(date_1), previous_year = floor_date(date_1, 'year'), next_year = ceiling_date(date_1, 'year') - 1, days_to_previous_year = as.integer(date_1 - previous_year), days_to_next_year = as.integer(next_year - date_1), rand_days_prev_year = sample.int(days_to_previous_year, 1), rand_days