在lubridate中将字符列转换为日期列

时间:2019-10-29 10:35:58

标签: r datetime tidyverse lubridate

我有一个数据框,看起来像:

# A tibble: 10 x 4
      id incoming_date expiry_date end_date
   <dbl> <chr>         <chr>       <chr>   
 1     1 11.17.18      10.1.19     03.1.19 
 2    11 03.4.19       NA          03.20.19
 3     2 03.17.19      02.1.20     05.7.19 
 4     2 05.7.19       NA          06.15.19
 5     4 06.11.19      05.1.21     06.22.19
 6     1 06.12.19      04.1.21     NA      
 7     2 06.12.19      04.1.21     NA      
 8    13 11.16.18      06.1.19     02.20.19
 9     7 02.19.19      12.1.21     3.23.19 
10     1 03.19.19      01.1.21     09.10.19

可复制:

library(tidyverse)
library(lubridate) 

df <-  as.tibble(structure(list(id = c(1, 11, 2, 2, 4, 1, 2, 13, 7, 1), incoming_date = c("11.17.18", 
    "03.4.19", "03.17.19", "05.7.19", "06.11.19", "06.12.19", "06.12.19", 
    "11.16.18", "02.19.19", "03.19.19"), expiry_date = c("10.1.19", 
    NA, "02.1.20", NA, "05.1.21", "04.1.21", "04.1.21", "06.1.19", 
    "12.1.21", "01.1.21"), end_date = c("03.1.19", "03.20.19", "05.7.19", 
    "06.15.19", "06.22.19", NA, NA, "02.20.19", "3.23.19", "09.10.19"
    )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
    )))

我要将列从字符转换为日期格式,如下所示:

      id incoming_date expiry_date end_date  
   <dbl> <date>        <date>      <date>    
 1     1 2018-11-17    2019-10-01  2019-03-01
 2    11 2019-03-04    NA          2019-03-20
 3     2 2019-03-17    2020-02-01  2019-05-07
 4     2 2019-05-07    NA          2019-06-15
 5     4 2019-06-11    2021-05-01  2019-06-22
 6     1 2019-06-12    2021-04-01  NA        
 7     2 2019-06-12    2021-04-01  NA        
 8    13 2018-11-16    2019-06-01  2019-02-20
 9     7 2019-02-19    2021-12-01  2019-03-23
10     1 2019-03-19    2021-01-01  2019-09-10

我试图做的-从incoming_date列开始-是:

df %>%
  mutate(incoming_date_parsed = strptime(as.POSIXct(incoming_date), "%m.%d.%Y"))

引发错误:

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

按照逻辑顺序,我要做的是:

  1. 将列从字符转换为日期时间
  2. 以YYYY-MM-dd格式解析以下日期

1 个答案:

答案 0 :(得分:0)

您真的很亲密! 在您的示例中,没有理由尝试先转换为posxict,然后再转换为日期。 并且您将在YY格式的年份中使用小写的“ y”。

App.js