I have an data set with many columns, one of which is year. For every row, I would like to randomly generate a date, such that it belongs to the year in that row.
library(data.table)
claims <- data.table(claimAmount = runif(n = 10, min = 0, max = 200),
claimYear = sample(x = 2014:2019, size = 10, replace = TRUE))
I have that someone posted this function as solution:
rdate <- function(x,
min = paste0(format(claims$claimYear, '%Y'), '-01-01'),
max = paste0(format(claims$claimYear, '%Y'), '-12-31'),
sort = TRUE) {
dates <- sample(seq(as.Date(min), as.Date(max), by = "day"), x, replace = TRUE)
if (sort == TRUE) {
sort(dates)
} else {
dates
}
}
and tried to apply it to by data, but recieved an error of "invalid trim argument":
apply(claims, 1, rdate)
Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, :
invalid 'trim' argument
How can I easily generate random dates in R?