【问题标题】:How do I make sure my column descends by year instead of the day of the month?如何确保我的列按年而不是按月下降?
【发布时间】:2022-02-09 06:11:19
【问题描述】:

鉴于我有 x 列

head(daily_negative_PRscore$x)
"2018-01-02 10:29:00 CET" "2017-01-03 18:28:00 CET" "2017-01-03 19:29:00 CET" "2018-01-03 00:00:00 CET" "2017-01-04 10:47:00 CET" "2017-01-04 11:05:00 CET"

我希望它下降的理想方式如下:

2017-01-03 18:28:00  2017-01-03 19:29:00 2017-01-04 00:00:00 ...

【问题讨论】:

  • daily_negative_PRscore <- daily_negative_PRscore[order(as.POSIXct(daily_negative_PRscore$x)),]
  • 非常感谢!!那行得通

标签: r date posixct


【解决方案1】:

另一种选择是使用arrange

library(tidyverse)

daily_negative_PRscore %>% 
  arrange(desc(as.POSIXct(x)))

或带有as.Date 的另一个基本 R 选项:

daily_negative_PRscore[rev(order(as.Date(daily_negative_PRscore$x, format = "%Y-%m-%d %H:%M:%S"))),]

输出

                        x
1 2018-01-03 00:00:00 CET
2 2018-01-02 10:29:00 CET
3 2017-01-04 11:05:00 CET
4 2017-01-04 10:47:00 CET
5 2017-01-03 19:29:00 CET
6 2017-01-03 18:28:00 CET

数据

daily_negative_PRscore <- structure(list(x = c("2018-01-02 10:29:00 CET", "2017-01-03 18:28:00 CET", 
                                               "2017-01-03 19:29:00 CET", "2018-01-03 00:00:00 CET", "2017-01-04 10:47:00 CET", 
                                               "2017-01-04 11:05:00 CET")), class = "data.frame", row.names = c(NA, 
                                                                                                                -6L))

【讨论】:

  • 这个是逐年递增的。 OP希望它逐年下降。包括descfucntion
  • @Onyambu 我一开始没有使用它,因为 cmets 中的答案没有使用 rev 并且 OP 说它有效。所以,猜想有点不清楚。但我继续更新假设他们确实需要它下降。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 2019-12-07
  • 2020-10-13
  • 2013-02-12
  • 2012-12-04
  • 2016-12-02
相关资源
最近更新 更多