【问题标题】:Generate random dates in given years在给定年份生成随机日期
【发布时间】:2020-12-13 04:57:53
【问题描述】:

我有一个包含许多列的数据集,其中一个是年份。对于每一行,我想随机生成一个日期,使其属于该行中的年份。

library(data.table)
claims <- data.table(claimAmount = runif(n = 10, min = 0, max = 200),
                     claimYear = sample(x = 2014:2019, size = 10, replace = TRUE))

我已经有人发布了这个功能作为解决方案:

    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
      }

}

并尝试按数据应用它,但收到“无效修剪参数”的错误:

 apply(claims, 1, rdate)
 Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L,  : 
  invalid 'trim' argument 

如何在 R 中轻松生成随机日期?

【问题讨论】:

  • 在生成随机数据时请始终使用 set.seed() 以实现可重复性。

标签: r date data.table


【解决方案1】:

因为你有一个data.table,你可以创建一个IDate,采样.N整数,添加它们,并通过引用创建新列。

claims[ , ddate := as.IDate(paste0(claimYear, "-01-01")) + sample(0:364, .N)]

 #    claimAmount claimYear      ddate
 # 1:   76.007036      2018 2018-11-20
 # 2:  155.489044      2015 2015-06-16
 # 3:  186.941046      2015 2015-05-09
 # 4:   42.428504      2019 2019-10-26
 # 5:  130.334753      2014 2014-09-27
 # 6:   25.111019      2017 2017-07-06
 # 7:   53.444134      2014 2014-11-03
 # 8:   77.222819      2017 2017-03-26
 # 9:    2.678067      2016 2016-10-03
 #10:   76.477591      2019 2019-11-26

【讨论】:

    【解决方案2】:

    使用lubridate::days

    as.POSIXct(paste0(claims$claimYear, "-01-01")) + 
      lubridate::days(sample(0:364, nrow(claims), TRUE))
     [1] "2018-11-15 GMT" "2019-09-02 GMT" "2016-08-02 GMT" "2018-08-02 GMT"   
     [5] "2016-01-09 GMT" "2015-07-05 GMT" "2016-12-19 GMT" "2019-03-04 GMT"  
     [9] "2014-01-06 GMT" "2018-06-17 GMT"
    

    【讨论】:

      【解决方案3】:

      由于这被标记为data.table

      claims[, rdate := sample(
                 seq.Date(
                   from = as.Date(paste0(claimYear,"0101"), "%Y%m%d"),
                   to = as.Date(paste0(claimYear,"1231"), "%Y%m%d"),
                   by = 1
                 ), 
                size = 1
              ),
             by = claimYear]
      
      #     claimAmount claimYear      rdate
      #  1:   176.47326      2018 2018-01-17
      #  2:    18.19580      2015 2015-11-09
      #  3:   142.48411      2015 2015-11-09
      #  4:    42.76339      2019 2019-08-02
      #  5:   177.12420      2014 2014-08-21
      #  6:    90.12079      2017 2017-06-07
      #  7:   193.57403      2016 2016-02-27
      #  8:   107.02539      2019 2019-08-02
      #  9:   119.92621      2017 2017-06-07
      # 10:   110.95285      2016 2016-02-27
      

      【讨论】:

        猜你喜欢
        • 2017-11-06
        • 2016-01-28
        • 2018-11-11
        • 2014-02-25
        • 2021-07-10
        • 2022-01-23
        • 2013-05-10
        • 2018-11-06
        相关资源
        最近更新 更多