【问题标题】:convert datetime strings and order by date转换日期时间字符串并按日期排序
【发布时间】:2019-01-05 01:08:43
【问题描述】:

我有一个非常重要的问题,即在 R 中将表示日期时间的字符串转换为 R 可以理解的对象(PO​​SIXct?)。

我有一个日期时间的字符向量,如下所示:

 [1] "Thu Apr 19 00:42:24 +0000 2018" "Sat Apr 14 03:08:30 +0000 2018" "Thu Apr 02 12:42:07 +0000 2015"
 [4] "Wed Apr 25 02:24:49 +0000 2018" "Sun Apr 03 00:37:19 +0000 2016" "Fri Apr 11 10:02:42 +0000 2014"
 [7] "Tue Jan 09 13:57:33 +0000 2018" "Wed Apr 13 09:45:05 +0000 2016" "Thu May 18 11:26:10 +0000 2017"
[10] "Thu Oct 05 03:41:32 +0000 2017"

我的目标是对这些值进行排序,使最近的日期位于顶部,最早的日期位于底部。据我所知,这将涉及将这些字符串转换为日期时间对象,但即使这一步我还没有开始工作。

我试过了:

lubridate::as_date(dates[1], tz = "UTC", format = NULL)
as.POSIXct(dates[1], tz = "UTC")

但我总是收到以下错误:

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

我认为我可以通过指定 format 参数来解决这个问题,但我该怎么做呢? 此外,一旦我转换了它们(或者,如果我不需要转换它们,则不这样做) - 我该如何对这些日期进行排序?

任何帮助将不胜感激, 提前致谢!

【问题讨论】:

  • 您的数据中的+0000 是什么?有什么想法吗?
  • as.POSIXct(dates, format="%a %b %d %H:%M:%S %z %Y") 为我工作

标签: r sorting date posixct


【解决方案1】:

这是一种删除无关的+0000 并使用正则表达式将年份移动到月份和日期旁边的方法,然后使用lubridate 的解析器获得所需的输出。如果您更喜欢正则表达式而不是记住strptime 代码...

library(stringr)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
dates <- c(
  "Thu Apr 19 00:42:24 +0000 2018", "Sat Apr 14 03:08:30 +0000 2018",
  "Thu Apr 02 12:42:07 +0000 2015", "Wed Apr 25 02:24:49 +0000 2018",
  "Sun Apr 03 00:37:19 +0000 2016", "Fri Apr 11 10:02:42 +0000 2014",
  "Tue Jan 09 13:57:33 +0000 2018", "Wed Apr 13 09:45:05 +0000 2016",
  "Thu May 18 11:26:10 +0000 2017", "Thu Oct 05 03:41:32 +0000 2017"
)

dates %>%
  str_replace_all("(^.{4})(.{6} )(.{8})( \\+0000 )(\\d{4})$", "\\2\\5 \\3") %>%
  mdy_hms()
#>  [1] "2018-04-19 00:42:24 UTC" "2018-04-14 03:08:30 UTC"
#>  [3] "2015-04-02 12:42:07 UTC" "2018-04-25 02:24:49 UTC"
#>  [5] "2016-04-03 00:37:19 UTC" "2014-04-11 10:02:42 UTC"
#>  [7] "2018-01-09 13:57:33 UTC" "2016-04-13 09:45:05 UTC"
#>  [9] "2017-05-18 11:26:10 UTC" "2017-10-05 03:41:32 UTC"

由reprex package (v0.2.0) 于 2018 年 7 月 27 日创建。

【讨论】:

  • 见my comment above (in MKR's answer),删除+0000是错误的做法。
  • 啊,我不知道那个信息是什么。这使得这种方法很笨拙。我不知道tz 解析器的lubridate 参数如何接受输入。
  • 老实说,我认为这是时区(经验丰富)。这就是许多时间戳格式中的数字偏移量(即使提供的字符串中的字段顺序是 inane ...它甚至不是破坏的美国顺序!)。一些偏移格式仅使用+00,但这会错过未按小时对齐的时区(例如,阿富汗、澳大利亚中部、...、shown here)。其他 TZ 格式从 "PST8PDT" 到 "US/Eastern" 不等,因此鼓励像 lubridate 这样的包来帮助理解这一切。 (夏令时更有趣......)
【解决方案2】:

或者我们可以使用order(as.Date())。

> dt[order(as.Date(dt, format="%a %b %d %H:%M:%S %z %Y"))]
 [1] "Fri Apr 11 10:02:42 +0000 2014" "Thu Apr 02 12:42:07 +0000 2015" "Sun Apr 03 00:37:19 +0000 2016"
 [4] "Wed Apr 13 09:45:05 +0000 2016" "Thu May 18 11:26:10 +0000 2017" "Thu Oct 05 03:41:32 +0000 2017"
 [7] "Tue Jan 09 13:57:33 +0000 2018" "Sat Apr 14 03:08:30 +0000 2018" "Thu Apr 19 00:42:24 +0000 2018"
[10] "Wed Apr 25 02:24:49 +0000 2018"

数据

dt <- c("Thu Apr 19 00:42:24 +0000 2018", "Sat Apr 14 03:08:30 +0000 2018" ,
        "Thu Apr 02 12:42:07 +0000 2015", "Wed Apr 25 02:24:49 +0000 2018", 
        "Sun Apr 03 00:37:19 +0000 2016", "Fri Apr 11 10:02:42 +0000 2014",
        "Tue Jan 09 13:57:33 +0000 2018" ,"Wed Apr 13 09:45:05 +0000 2016" ,
        "Thu May 18 11:26:10 +0000 2017","Thu Oct 05 03:41:32 +0000 2017")

【讨论】:

  • 亲爱的@jaySf - 这工作得很好。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 2015-09-17
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多