我有一个 POSIXct 值的向量,我想将它们四舍五入到最近的 15 分钟。我不关心日期。如何将这些值转换为小时和分钟?
例如,我希望将值
"2012-05-30 20:41:21 UTC"
成为
"20:45"
确实,这是一个有一些有用的回答的老问题。到目前为止,giraffhere的最后一个回答似乎是最优雅的。然而,解决问题的不是floor_date而是round_date:
lubridate::round_date(x, "15 minutes")
round
函数。诀窍在于在舍入之前将数值除以900秒(15分钟*60秒),然后在舍入后乘以900秒:a <-as.POSIXlt("2012-05-30 20:41:21 UTC")
b <-as.POSIXlt(round(as.double(a)/(15*60))*(15*60),origin=(as.POSIXlt('1970-01-01')))
b
[1] "2012-05-30 20:45:00 EDT"
要仅获取小时和分钟,只需使用格式
format(b,"%H:%M")
[1] "20:45"
as.character(format(b,"%H:%M"))
[1] "20:45"
类似于什么东西
format(strptime("1970-01-01", "%Y-%m-%d", tz="UTC") + round(as.numeric(your.time)/900)*900,"%H:%M")
能够运行
虽然这是一个旧问题,但我想指出现在使用lubridate
包很容易地处理这个问题,只需使用floor_date
函数将一组POSIXct对象切割成15分钟的间隔。
做法如下:x <- lubridate::floor_date(x, "15 minutes")
编辑:用户@user3297928指出,要将时间舍入到最近的15分钟,请使用lubridate::round_date(x, "15 minutes")
。上面的代码是向下取整。
align.time
函数来处理四舍五入,然后使用format
函数返回一个"HH:MM"字符串:R> library(xts)
R> p <- as.POSIXct("2012-05-30 20:41:21", tz="UTC")
R> a <- align.time(p, n=60*15) # n is in seconds
R> format(a, "%H:%M")
[1] "20:45"
align.time
只会向上舍入,而你想要四舍五入到最近的15分钟。抱歉。 - Joshua Ulrichalign.time(p - lubridate :: minutes(15), n = 60 * 15)
- Nicholas Hamilton# install only if you don't have
install.packages(c("microbenchmarkCore","data.table"),
repos = c("https://olafmersmann.github.io/drat",
"https://jangorecki.github.io/drat/iperiod"))
library(microbenchmarkCore)
library(data.table) # iunit branch
library(xts)
Sys.setenv(TZ="UTC")
## some source data: download and unzip csv
# "http://api.bitcoincharts.com/v1/csv/btceUSD.csv.gz"
# below benchmark on btceUSD.csv.gz 11-Oct-2015 11:35 133664801
system.nanotime(dt <- fread(".btceUSD.csv"))
# Read 21931266 rows and 3 (of 3) columns from 0.878 GB file in 00:00:10
# user system elapsed
# NA NA 9.048991
# take the timestamp only
x = as.POSIXct(dt[[1L]], tz="UTC", origin="1970-01-01")
# functions
shhhhi <- function(your.time){
strptime("1970-01-01", "%Y-%m-%d", tz="UTC") + round(as.numeric(your.time)/900)*900
}
PLapointe <- function(a){
as.POSIXlt(round(as.double(a)/(15*60))*(15*60),origin=(as.POSIXlt('1970-01-01')))
}
# myRound - not vectorized
# compare results
all.equal(
format(shhhhi(x),"%H:%M"),
format(PLapointe(x),"%H:%M")
)
# [1] TRUE
all.equal(
format(align.time(x, n = 60*15),"%H:%M"),
format(periodize(x, "mins", 15),"%H:%M")
)
# [1] TRUE
# IPeriod native input are IDate and ITime - will be tested too
idt <- IDateTime(x)
idate <- idt$idate
itime <- idt$itime
microbenchmark(times = 10L,
shhhhi(x),
PLapointe(x),
xts = align.time(x, 15*60),
posix_ip_posix = as.POSIXct(periodize(x, "mins", 15), tz="UTC"),
posix_ip = periodize(x, "mins", 15),
ip_posix = as.POSIXct(periodize(idate, itime, "mins", 15), tz="UTC"),
ip = periodize(idate, itime, "mins", 15))
# Unit: microseconds
# expr min lq mean median uq max neval
# shhhhi(x) 960819.810 984970.363 1127272.6812 1167512.2765 1201770.895 1243706.235 10
# PLapointe(x) 2322929.313 2440263.122 2617210.4264 2597772.9825 2792936.774 2981499.356 10
# xts 453409.222 525738.163 581139.6768 546300.9395 677077.650 767609.155 10
# posix_ip_posix 3314609.993 3499220.920 3641219.0876 3586822.9150 3654548.885 4457614.174 10
# posix_ip 3010316.462 3066736.299 3157777.2361 3133693.0655 3234307.549 3401388.800 10
# ip_posix 335.741 380.696 513.7420 543.3425 630.020 663.385 10
# ip 98.031 151.471 207.7404 231.8200 262.037 278.789 10
IDate
和 ITime
不仅在这个特定任务中成功扩展。同样像 IPeriod
一样,它们都是基于整数的。我认为它们在按 datetime 字段进行连接或分组时也会很好地扩展。
在线手册:https://jangorecki.github.io/drat/iperiod/
%/%
运算符处理它,将期间硬编码在那里甚至不需要存储任何属性,只是一个数字,没有黑魔法。 - jangorecki round.POSIXt()
和trunc.POSIXt()
的操作。myRound <- function (x, convert = TRUE) {
x <- as.POSIXlt(x)
mins <- x$min
mult <- mins %/% 15
remain <- mins %% 15
if(remain > 7L || (remain == 7L && x$sec > 29))
mult <- mult + 1
if(mult > 3) {
x$min <- 0
x <- x + 3600
} else {
x$min <- 15 * mult
}
x <- trunc.POSIXt(x, units = "mins")
if(convert) {
x <- format(x, format = "%H:%M")
}
x
}
> tmp <- as.POSIXct("2012-05-30 20:41:21 UTC")
> myRound(tmp)
[1] "20:45"
> myRound(tmp, convert = FALSE)
[1] "2012-05-30 20:45:00 BST"
> tmp2 <- as.POSIXct("2012-05-30 20:55:21 UTC")
> myRound(tmp2)
[1] "21:00"
> myRound(tmp2, convert = FALSE)
[1] "2012-05-30 21:00:00 BST"
structure(c(1313331280, 1313334917, 1313334917, 1313340309, 1313340309, 1313340895, 1313340895, 1313341133, 1313341218, 1313341475), class = c("POSIXct", "POSIXt"), tzone = "UTC")
。 - jangorecki
b <- as.POSIXlt(round(as.numeric(a)/(15*60))*(15*60), origin='1970-01-01')
。 - Mark RajcokR4.3.*
版本以后,如果是Unix纪元的情况下,我们不再需要显式地指定起始点。 - jay.sf