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.
months(sample(1:12, nrow(df))) - days(sample(1:31, nrow(df)))dplyrpipeline, perhaps replace,1with,n()in your two calls tosample.yearsfunction does not return a date but rather a number, which is not going to correspond with the scale of Date values that are returned byymd. You might get closer by multiplying by 365 for years, 30 for months, and making the month and date samples the correct length.