如何确保我的列按年而不是每月的日期下降?
How do I make sure my column descends by year instead of the day of the month?
鉴于我有列 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 ...
另一种选择是使用 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))
鉴于我有列 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 ...
另一种选择是使用 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))