r - 将持续时间转换为 ISO 8601 格式
问题描述
是否有将持续时间(来自 lubridate::duration 的输出)转换为 ISO8601 持续时间格式的功能?例如
lubridate::duration("PT1H3M")
# "3780s (~1.05 hours)"
但我将如何得到相反的结果?例如输入是
"3780s (~1.05 hours)"
要得到:
"PT1H3M"
参考:ISO8601 持续时间:
解决方案
包中的函数适用format_ISO8601()
于lubridate
日期和持续时间:
my_duration <- lubridate::duration("PT1H3M")
my_duration
# [1] "3780s (~1.05 hours)"
lubridate::format_ISO8601(my_duration)
# [1] "PT3780S"
但请注意,基础格式不是字符串(正如您的问题可能暗示的那样):
str(my_duration)
# Formal class 'Duration' [package "lubridate"] with 1 slot
# ..@ .Data: num 3780
lubridate::format_ISO8601("3780s (~1.05 hours)")
# Error in (function (classes, fdef, mtable) :
# unable to find an inherited method for function ‘format_ISO8601’ for signature ‘"character"’
推荐阅读
- android - 在 Google Play >封闭测试版<应用程序中使用 Facebook 登录
- r - 在整洁的语料库 R 中快速搜索字符串
- momentjs - 如何使用 Momentjs 从特定日期查找前一个工作日(周日、周一等)
- npm - 500 内部服务器错误:npm@latest
- sql - 使用两组标准连接两个数据库
- php - Laravel Slack 通知,开始一个线程
- c# - C# Exchange - 将消息转发为
- c# - C# LinqToTwitter - 大多数推文返回空媒体实体
- oracle - 如何使用 Oracle SQL 11g 创建具有已创建列的动态数据透视表
- javascript - 打字稿:胖箭头和这个范围关系