【问题标题】:How can I sort POSIXct datetime format in R?如何在 R 中对 POSIXct 日期时间格式进行排序?
【发布时间】:2021-12-22 03:39:21
【问题描述】:

我有一个包含 4 列的数据框,其中第一列称为 Date_et_heure。在该专栏中,我有一个 POSIXct POSIXt 类日期时间,格式为“%Y-%m-%d %H:%M:%S”。我想安排我的数据框,使行始终按时间顺序排列。

我尝试使用arrange()函数但它不能接受POSIXct POSIXt 格式数据;我总是得到错误:

UseMethod("arrange") 中的错误: 没有适用于“排列”的方法应用于“c('POSIXct', 'POSIXt') 类的对象

我尝试使用 order() 函数,为此我需要使用 as.Date() 函数转换 POSIXct。但是 as.Date() 会忽略该列的时间(“%H:%M:%S”)格式。

有谁知道是否有订购 POSIXct 类数据的方法?希望有一个可靠的转换。

谢谢!

【问题讨论】:

  • 试试i <- order(df$Date_et_heure); df <- df[i,]

标签: r posixct


【解决方案1】:

发布您的代码,错误消息并不表明问题是您的对象具有提到的类,而是您为在这种​​情况下碰巧具有该类的对象提供了不适用的方法。

问题不在于 dplyr 功能,正如其他回复中的示例所示。

这里是 POSIXlt 和 POSIXct 的示例(它们都有类“POSIXct”“POSIXt”)。您可以同时按两种方式进行排序。

df <- data.frame(
  Date_et_heurePOSIXct = sample(seq(as.POSIXct('2021-08-01'), as.POSIXct('2021-11-09', tz = "UTC"), by = "1 sec"), 5),
  Date_et_heurePOSIXlt = sample(seq(as.POSIXlt('2021-08-01'), as.POSIXlt('2021-11-09', tz = "UTC"), by = "1 sec"), 5)
)

df %>% arrange(Date_et_heurePOSIXct)
df %>% arrange(desc(Date_et_heurePOSIXct))
df %>% arrange(Date_et_heurePOSIXlt)
df %>% arrange(desc(Date_et_heurePOSIXlt))


class(df$Date_et_heurePOSIXct)
class(df$Date_et_heurePOSIXlt)

【讨论】:

  • 代码很简单:FinalExcel$Date_et_heure
  • 你用错了排列方式,你的数据可能没问题。试试这个:FinalExcel % 安排(Date_et_heure)或 FinalExcel
【解决方案2】:

orderdplyr::arrange 都可以对 "POSIXct" 对象进行排序。

i <- order(df1$Date_et_heure)
df1[i,]
#         Date_et_heure x
#1  2021-11-09 12:41:57 i
#2  2021-11-09 12:41:58 d
#3  2021-11-09 12:41:59 j
#4  2021-11-09 12:42:00 e
#5  2021-11-09 12:42:01 h
#6  2021-11-09 12:42:02 b
#7  2021-11-09 12:42:03 a
#8  2021-11-09 12:42:04 f
#9  2021-11-09 12:42:05 c
#10 2021-11-09 12:42:06 g

df1 |> dplyr::arrange(Date_et_heure)
#         Date_et_heure x
#1  2021-11-09 12:41:57 i
#2  2021-11-09 12:41:58 d
#3  2021-11-09 12:41:59 j
#4  2021-11-09 12:42:00 e
#5  2021-11-09 12:42:01 h
#6  2021-11-09 12:42:02 b
#7  2021-11-09 12:42:03 a
#8  2021-11-09 12:42:04 f
#9  2021-11-09 12:42:05 c
#10 2021-11-09 12:42:06 g

测试数据

set.seed(2021)
n <- 10
Date_et_heure <- Sys.time() + sample(n)
df1 <- data.frame(Date_et_heure, x = letters[1:n])

【讨论】:

    【解决方案3】:

    POSIXct 在 R 中既有用又强大。在内部,它“只是”一个 double,您可以直接对它们使用所有常规操作。

    这是一个最小的基础 R 演示:

    > set.seed(123)    # reproducible
    > v <- as.POSIXct(Sys.time() + rnorm(5)*3600)
    > v                # random draw around 'now', not sorted
    [1] "2021-11-09 06:05:15.009926 CST" "2021-11-09 06:25:04.083292 CST" 
    [3] "2021-11-09 08:12:24.072185 CST" "2021-11-09 06:43:06.552463 CST" 
    [5] "2021-11-09 06:46:38.158100 CST"
    > diff(v)          # not sorted -> pos. and neg. differences
    Time differences in mins
    [1]  19.81789 107.33315 -89.29200   3.52676
    >
    

    所以这里使用order()重新排列:

    > w <- v[order(v)]
    > w
    [1] "2021-11-09 06:05:15.009926 CST" "2021-11-09 06:25:04.083292 CST" 
    [3] "2021-11-09 06:43:06.552463 CST" "2021-11-09 06:46:38.158100 CST"
    [5] "2021-11-09 08:12:24.072185 CST"
    > diff(w)
    Time differences in mins
    [1] 19.81789 18.04115  3.52676 85.76523
    > 
    

    这按预期安排了时间戳。

    【讨论】:

      猜你喜欢
      • 2018-01-19
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 2014-08-21
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多