将原始日期转换为R中的序数整数

时间:2019-05-16 13:33:27

标签: r date integer

我有带日期的数据框,我想将日期转换为序数整数

> glimpse(df)
Observations: 211,797
Variables: 42
   $ Churn                  <fct> No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No, No…
    $ min_date               <date> 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2…
    $ min_NoPaid_date        <date> 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2017-02-13, 2…
    $ min_paid_date          <date> 2017-02-27, 2017-02-27, 2017-02-27, 2017-02-27, 2017-02-27, 2017-02-27, 2017-02-27, 2017-02-27, 2017-02-27, 2…
    $ max_date               <date> 2019-05-14, 2019-05-14, 2019-05-14, 2019-05-14, 2019-05-14, 2019-04-27, 2019-05-14, 2019-05-14, 2019-05-14, 2…
    $ max_NoPaid_date        <date> 2017-02-26, 2017-02-26, 2017-02-26, 2017-02-26, 2017-02-26, 2019-04-27, 2017-02-26, 2017-02-26, 2017-02-26, 2…
    $ max_paid_date          <date> 2019-05-14, 2019-05-14, 2019-05-14, 2019-05-14, 2019-05-14, 2019-04-26, 2019-05-14, 2019-05-14, 2019-05-14, 2…


> str(df)
    Classes ‘data.table’ and 'data.frame':  211797 obs. of  42 variables:
9173842 11288931 9167070 4310995 11289083 8816781 ...
     $ Churn                 : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 359 360 361 362 363 364 365 366 367 368 ...
     $ min_date              : Date, format: "2017-02-13" "2017-02-13" "2017-02-13" ...
     $ min_NoPaid_date       : Date, format: "2017-02-13" "2017-02-13" "2017-02-13" ...
     $ min_paid_date         : Date, format: "2017-02-27" "2017-02-27" "2017-02-27" ...
     $ max_date              : Date, format: "2019-05-14" "2019-05-14" "2019-05-14" ...
     $ max_NoPaid_date       : Date, format: "2017-02-26" "2017-02-26" "2017-02-26" ...
     $ max_paid_date         : Date, format: "2019-05-14" "2019-05-14" "2019-05-14" ...

我试图制作一个函数来应用所有日期列。

date_to_integer <- function(x) {
                        y = as.integer(format(as.Date(x), "%Y%m%d"))
                        y
    }


churn_data_tbl[4:9] <- lapply(churn_data_tbl, date_to_integer)

但是,我得到Error in charToDate(x) : character string is not in a standard unambiguous format

有人可以帮我解决这个问题吗?

预先感谢

0 个答案:

没有答案