Tony
Tony

Reputation: 13

How to randomize a date in R

I'm trying to back into a fake birthdate based on the age of a consumer. I'm using lubridate package. Here is my code:

ymd(today) - years(df$age) - months(sample(1:12, 1)) - days(sample(1:31, 1)).

I want to use this to generate a different dob that equals the age. When I run this inline it gives every row the same month and day and different year. I want the month and day to vary as well.

Upvotes: 0

Views: 193

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389047

In base R, we can extract the year from the age column subtract it from current year, select a random month and date, paste the values together and create a Date object.

set.seed(123)
df <- data.frame(age = sample(100, 5))

as.Date(paste(as.integer(format(Sys.Date(), "%Y")) - df$age, 
      sprintf("%02d", sample(12, nrow(df))),  
      sprintf("%02d", sample(30, nrow(df))), sep = "-"))

#[1] "1990-01-29" "1940-06-14" "1978-09-19" "1933-05-16" "1928-04-03"

However, in this case you might need to make an extra check for month of February, or to be safe you might want to sample dates only from 28 instead of 30 here.

Upvotes: 0

Emer
Emer

Reputation: 3824

You can make a date with the year of birth at 1st of January and then add random duration of days to it.

library(lubridate)
library(dplyr)
set.seed(5)

df <- data.frame(age = c(18, 33, 58, 63))

df %>%
  mutate(dob = make_date(year(Sys.Date()) - age, 1, 1) +
                           duration(sample(0:364, n()), unit = "days"))

Upvotes: 3

Related Questions