trinker / wakefield

Generate random data sets
256 stars 28 forks source link

Start date End date #26

Open zx8754 opened 2 years ago

zx8754 commented 2 years ago

There is already dob, date_stamp, and we could use these to get start and end dates, e.g.:

set.seed(1)
r_data_frame(
  n = 5,
  dob(name = "date_start")) %>% 
  mutate(date_end = date_start + runif(n(), 1, 100))

# # A tibble: 5 x 2
#   date_start date_end  
#   <date>     <date>    
# 1 2007-04-20 2007-06-01
# 2 2007-03-23 2007-06-16
# 3 2008-06-06 2008-07-29
# 4 2007-02-21 2007-04-17
# 5 2008-05-08 2008-07-31

Could we have a dedicated function that would create start and end date, where obviously date_end can't be before the date_start?

Or maybe even better, let date_stamp accept column name for start=, like below:

r_data_frame(
  n = 5,
  dob(name = "date_start")) %>% 
  mutate(date_end = date_stamp(start = date_start))
# Error in seq(start, length = k, by = by) : object 'date_start' not found
hadley commented 1 year ago

Did you mean this?

library(wakefield)

r_data_frame(
  n = 5,
  dob(name = "date_start"),
  date_stamp(start = date_start)
)
#> Error in seq(start, length = k, by = by): object 'date_start' not found

Created on 2022-10-03 with reprex v2.0.2

If so, this could be made to work by having r_data_frame() compute each column iteratively.