我正在尝试使用data.table包中的滚动连接,但我似乎无法获得我想要的输出。
我的数据是:
library(data.table)
dt <- fread(' datetime price
"2016-05-01 18:58:49.078" 2059.25
"2016-05-01 18:58:49.078" 2059.25
"2016-05-01 18:58:49.078" 2059.25
"2016-05-01 18:58:49.078" 2059.25
"2016-05-01 18:58:51.085" 2059.25
"2016-05-01 18:58:51.085" 2059.25
"2016-05-01 18:58:51.085" 2059.25
"2016-05-01 18:58:51.085" 2059.25
"2016-05-01 18:58:51.085" 2059.25
"2016-05-01 18:58:51.085" 2059.25
"2016-05-01 18:58:51.085" 2059.25
"2016-05-01 18:58:53.703" 2059.25
"2016-05-01 18:58:53.757" 2059.25
"2016-05-01 18:58:53.757" 2059.25
"2016-05-01 18:58:53.757" 2059.25
"2016-05-01 18:58:54.155" 2059.50
"2016-05-01 18:59:07.013" 2059.25
"2016-05-01 18:59:07.013" 2059.25
"2016-05-01 18:59:07.015" 2059.25
"2016-05-01 18:59:08.604" 2059.25
"2016-05-01 18:59:31.500" 2059.50
"2016-05-01 18:59:40.723" 2059.25
"2016-05-01 18:59:40.723" 2059.25
"2016-05-01 19:00:00.003" 2059.50
"2016-05-01 19:00:00.003" 2059.50
"2016-05-01 19:00:00.003" 2059.50
"2016-05-01 19:00:00.359" 2059.50
"2016-05-01 19:00:00.381" 2059.50
"2016-05-01 19:00:02.390" 2059.50
"2016-05-01 19:00:04.355" 2059.50
"2016-05-01 19:00:06.230" 2059.50', header = T)
dt$datetime <- as.POSIXct(dt$datetime)
我想知道每分钟最近的价格:
dt_minutes <- data.table(datetime = c(as.POSIXct("2016-05-01 18:59:00"),as.POSIXct("2016-05-01 19:00:00"),as.POSIXct("2016-05-01 19:01:00")))
> dt_minutes
datetime
1: 2016-05-01 18:59:00
2: 2016-05-01 19:00:00
3: 2016-05-01 19:01:00
我得到的输出是:
> dt[dt_minutes, roll = TRUE, on = "datetime"]
datetime price
1: 2016-05-01 18:59:00 2059.5
2: 2016-05-01 19:00:00 2059.5
3: 2016-05-01 19:00:00 2059.5
4: 2016-05-01 19:00:00 2059.5
5: 2016-05-01 19:01:00 2059.5
但我希望:
1: 2016-05-01 18:59:00 2059.5
2: 2016-05-01 19:00:00 2059.25
5: 2016-05-01 19:01:00 2059.5
有人知道为什么我的输出中会重复“2016-05-01 19:00:00”并且当时的价格错误了吗?
答案 0 :(得分:1)
在评论中建立弗兰克的答案,如果你运行<?php
$states = array("Alabama","Alaska","Arizona","Arkansas",
"California","Colorado","Connecticut","Delaware",
"Florida","Georgia","Hawaii","Idaho",
"Illinois","Indiana","Iowa","Kansas","Kentucky");
$stateAbbr = array("AL","AK","AZ","AR","CA","CO","CT","DE",
"FL","GA","HI","ID","IL","IN","IA","KS","KY");
?>
<!DOCTYPE html>
<html>
<body>
<h1>List of States</h1>
</body>
</html>
,你将获得你想要的结果。
另请注意,您可以在setNumericRounding(0)
中存储datetime
变量的副本,以查看从滚动的时间,以匹配dt
中的密钥,这也应该首先明确它是如何四舍五入的:
dt_minutes