将日期列表转换为 R 中的另一种格式?
Converting a list of dates into another format in R?
我正在尝试将这些列表更改为另一种格式,以便在 RJDBC 包中使用。 Oracle 的语法将只接受格式为“1-OCT-18”的日期。但是,此列表的输出格式为“2018-10-01”。
如何将这些列表转换为以下格式:1-OCT-18
begin <- ymd("2017-01-01")+ months(0:21)
last <- ymd("2017-02-01")+ months(0:22)-days(1)
您可以使用 format
,大写用 toupper
包装,trimws
到 trim [=15 创建的白色 space =].我在 format
中使用了 %e
,因为它是月份中的 十进制数 (1–31),前导 space 表示一位数。 那么我们就把白色的去掉space。
trimws(toupper(format(begin, "%e-%b-%y")))
# [1] "1-JAN-17" "1-FEB-17" "1-MAR-17" "1-APR-17" "1-MAY-17" "1-JUN-17"
# [7] "1-JUL-17" "1-AUG-17" "1-SEP-17" "1-OCT-17" "1-NOV-17" "1-DEC-17"
# [13] "1-JAN-18" "1-FEB-18" "1-MAR-18" "1-APR-18" "1-MAY-18" "1-JUN-18"
# [19] "1-JUL-18" "1-AUG-18" "1-SEP-18" "1-OCT-18"
对于 last
向量,您可以省略 trimws
因为 %e
不会为两位数生成任何白色 space。
toupper(format(last, "%e-%b-%y"))
# [1] "31-JAN-17" "28-FEB-17" "31-MAR-17" "30-APR-17" "31-MAY-17"
# [6] "30-JUN-17" "31-JUL-17" "31-AUG-17" "30-SEP-17" "31-OCT-17"
# [11] "30-NOV-17" "31-DEC-17" "31-JAN-18" "28-FEB-18" "31-MAR-18"
# [16] "30-APR-18" "31-MAY-18" "30-JUN-18" "31-JUL-18" "31-AUG-18"
# [21] "30-SEP-18" "31-OCT-18" "30-NOV-18"
我正在尝试将这些列表更改为另一种格式,以便在 RJDBC 包中使用。 Oracle 的语法将只接受格式为“1-OCT-18”的日期。但是,此列表的输出格式为“2018-10-01”。
如何将这些列表转换为以下格式:1-OCT-18
begin <- ymd("2017-01-01")+ months(0:21)
last <- ymd("2017-02-01")+ months(0:22)-days(1)
您可以使用 format
,大写用 toupper
包装,trimws
到 trim [=15 创建的白色 space =].我在 format
中使用了 %e
,因为它是月份中的 十进制数 (1–31),前导 space 表示一位数。 那么我们就把白色的去掉space。
trimws(toupper(format(begin, "%e-%b-%y")))
# [1] "1-JAN-17" "1-FEB-17" "1-MAR-17" "1-APR-17" "1-MAY-17" "1-JUN-17"
# [7] "1-JUL-17" "1-AUG-17" "1-SEP-17" "1-OCT-17" "1-NOV-17" "1-DEC-17"
# [13] "1-JAN-18" "1-FEB-18" "1-MAR-18" "1-APR-18" "1-MAY-18" "1-JUN-18"
# [19] "1-JUL-18" "1-AUG-18" "1-SEP-18" "1-OCT-18"
对于 last
向量,您可以省略 trimws
因为 %e
不会为两位数生成任何白色 space。
toupper(format(last, "%e-%b-%y"))
# [1] "31-JAN-17" "28-FEB-17" "31-MAR-17" "30-APR-17" "31-MAY-17"
# [6] "30-JUN-17" "31-JUL-17" "31-AUG-17" "30-SEP-17" "31-OCT-17"
# [11] "30-NOV-17" "31-DEC-17" "31-JAN-18" "28-FEB-18" "31-MAR-18"
# [16] "30-APR-18" "31-MAY-18" "30-JUN-18" "31-JUL-18" "31-AUG-18"
# [21] "30-SEP-18" "31-OCT-18" "30-NOV-18"