【问题标题】:Creating a sequence of dates with a special format创建具有特殊格式的日期序列
【发布时间】:2020-07-23 19:17:11
【问题描述】:

我想知道如何创建以下格式的日期序列:从 "Jul 23 10:20""Jul 30 10:25""1" 天?

我尝试了以下方法但没有成功:

seq.Date(as.Date("Jul 23 10:20"), as.Date("Jul 30 10:25"), length.out = 7)

【问题讨论】:

  • 你不想要 length.out=8 吗?

标签: r regex date sequence


【解决方案1】:

要保留时间,您应该转换为实际的日期时间,这样很容易构建一个每次增加 1 天的序列。您可以使用strftime 随意格式化。

strftime(seq(as.POSIXct("2020-07-23 10:20"), by = "1 day", length.out = 7), "%b %e %H:%M")
#> [1] "Jul 23 10:20" "Jul 24 10:20" "Jul 25 10:20" "Jul 26 10:20" "Jul 27 10:20"
#> [6] "Jul 28 10:20" "Jul 29 10:20"

【讨论】:

    【解决方案2】:

    我们可以通过转换为Date格式来获取日期

    format(seq(as.POSIXct("Jul 23 10:20", format = "%b %d %H:%M" ),
          by = "1 day", length.out = 7), "%b %d %H:%M")
    #[1] "Jul 23 10:20" "Jul 24 10:20" "Jul 25 10:20" "Jul 26 10:20" "Jul 27 10:20" "Jul 28 10:20" "Jul 29 10:20"
    

    lubridate

    library(lubridate)
    format(as.POSIXct("Jul 23 10:20", format = '%b %d %H:%M') + days(0:4), "%b %d %H:%M")
    #[1] "Jul 23 10:20" "Jul 24 10:20" "Jul 25 10:20" "Jul 26 10:20" "Jul 27 10:20"
    

    【讨论】:

    • @rnorouzian 抱歉,我正在查看您的输入,其中有两个输入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2013-02-22
    相关资源
    最近更新 更多