如何从 POSIXct 时间戳向量在 R 中创建日期部分?
How to create day part in R from a vector of POSIXct timestamps?
我有一长串 POSIXct 时间戳。我想return,为每个记录一天的一部分,包括星期几。
例如:
[1] Sunday night
[2] Sunday night
[3] Sunday afternoon
[4] Saturday night
[5] Sunday afternoon
...等等。
下面是一小部分数据:
my_dates <- as.POSIXct(c("2000-03-12 19:40:00 AEDT",
"2000-03-19 17:40:00 AEDT",
"2000-03-26 14:10:00 AEST",
"2000-04-01 19:40:00 AEST",
"2000-04-09 14:10:00 AEST",
"2000-04-16 14:40:00 AEST",
"2000-04-22 19:40:00 AEST",
"2000-04-30 14:10:00 AEST",
"2000-05-07 14:10:00 AEST",
"2000-05-14 14:10:00 AEST"))
我尝试了很多不同的方法,但我总是以丢失日期格式告终。
如有任何帮助,我们将不胜感激。
谢谢
一些创意 format
ting 和 cut
ting 应该可以让你到达那里:
paste(
format(my_dates, "%A"),
c("night","morn","afternoon","night")[
cut(as.numeric(format(my_dates,"%H")), c(0,5,11,17,23))
]
)
# [1] "Sunday night" "Sunday afternoon" "Sunday afternoon" "Saturday night"
# [5] "Sunday afternoon" "Sunday afternoon" "Saturday night" "Sunday afternoon"
# [9] "Sunday afternoon" "Sunday afternoon"
我有一长串 POSIXct 时间戳。我想return,为每个记录一天的一部分,包括星期几。 例如:
[1] Sunday night
[2] Sunday night
[3] Sunday afternoon
[4] Saturday night
[5] Sunday afternoon
...等等。
下面是一小部分数据:
my_dates <- as.POSIXct(c("2000-03-12 19:40:00 AEDT",
"2000-03-19 17:40:00 AEDT",
"2000-03-26 14:10:00 AEST",
"2000-04-01 19:40:00 AEST",
"2000-04-09 14:10:00 AEST",
"2000-04-16 14:40:00 AEST",
"2000-04-22 19:40:00 AEST",
"2000-04-30 14:10:00 AEST",
"2000-05-07 14:10:00 AEST",
"2000-05-14 14:10:00 AEST"))
我尝试了很多不同的方法,但我总是以丢失日期格式告终。
如有任何帮助,我们将不胜感激。 谢谢
一些创意 format
ting 和 cut
ting 应该可以让你到达那里:
paste(
format(my_dates, "%A"),
c("night","morn","afternoon","night")[
cut(as.numeric(format(my_dates,"%H")), c(0,5,11,17,23))
]
)
# [1] "Sunday night" "Sunday afternoon" "Sunday afternoon" "Saturday night"
# [5] "Sunday afternoon" "Sunday afternoon" "Saturday night" "Sunday afternoon"
# [9] "Sunday afternoon" "Sunday afternoon"