在R中设置时间间隔的起点

时间:2018-01-29 16:25:55

标签: r time format

我有不同的数据集,格式如下

Time                    Value1 Value2 ....
11/04/2015  15:12:22    1      2      ....
11/04/2015  15:13:46    1      2      ....

我想以15分钟的间隔对它们进行分组。我可以使用以下代码

执行此操作
data$time = cut(data$time, breaks = "15 min")
data.grouped <- aggregate(data[,c(-1)], by = list(time = datos$time), median)

问题是输出中的时间字段具有以下值

12/04/2015 16:12
12/04/2015 16:27
12/04/2015 16:42
12/04/2015 16:57

我希望时间是:00:15:30或:45。有没有办法强制间隔像这样或不同的方法来合并允许它的数据?

来自dput的示例数据:

structure(list(time = structure(list(sec = c(49, 5, 21, 37, 54, 
10, 38), min = c(12L, 13L, 13L, 13L, 13L, 14L, 22L), hour = c(15L, 
15L, 15L, 15L, 15L, 15L, 16L), mday = c(11L, 11L, 11L, 11L, 11L, 
11L, 12L), mon = c(3L, 3L, 3L, 3L, 3L, 3L, 3L), year = c(116L, 
116L, 116L, 116L, 116L, 116L, 116L), wday = c(1L, 1L, 1L, 1L, 
1L, 1L, 2L), yday = c(101L, 101L, 101L, 101L, 101L, 101L, 102L
), isdst = c(1L, 1L, 1L, 1L, 1L, 1L, 1L), zone = c("CEST", "CEST", 
"CEST", "CEST", "CEST", "CEST", "CEST"), gmtoff = c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_)), .Names = c("sec", "min", "hour", "mday", "mon", 
"year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", 
"POSIXt")), value1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("time", 
"value1"), row.names = c(NA, -7L), class = "data.frame")

2 个答案:

答案 0 :(得分:2)

dput开始,将其称为df,我们首先将您的因素转换为POSIXct课程,然后我们floor将其调整为最接近的round分钟以下。 (如果您想要最接近的15分钟,请使用floor代替df$time = as.POSIXct(df$time) df$time15 = lubridate::floor_date(df$time, unit = "15 min") df # time value1 time15 # 1 2016-04-11 15:12:49 0 2016-04-11 15:00:00 # 2 2016-04-11 15:13:05 0 2016-04-11 15:00:00 # 3 2016-04-11 15:13:21 0 2016-04-11 15:00:00 # 4 2016-04-11 15:13:37 0 2016-04-11 15:00:00 # 5 2016-04-11 15:13:54 0 2016-04-11 15:00:00 # 6 2016-04-11 15:14:10 0 2016-04-11 15:00:00 # 7 2016-04-12 16:22:38 0 2016-04-12 16:15:00

time15

然后,您可以使用var player = { // These keep track of player input: -1 and 1 are opposite directions, 0 is no input roll: 0, pitch: 0, yaw: 0, radians: [0, 0, 0], // X, Y, Z // Keep track of the rotational axes axisX: [1, 0, 0], axisY: [0, 1, 0], axisZ: [0, 0, 1], // 4D matrices scale: [ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ], trans: [ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ], rotX: [ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ], rotY: [ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ], rotZ: [ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ] }; /* This also updates the player's radians, so I use it for both the matrix and quaternion transforms "mod" is a timing modifier to account for varying framerates */ function updateMatrix3D(mod) { var rad, sin, cos; if (player.yaw) { player.axisY[0] = Math.sin(player.radians[0]); player.axisY[1] = Math.cos(player.radians[1]); player.axisY[2] = Math.sin(player.radians[2]); rad = player.radians[1] + mod * player.yaw / 40; if (rad >= TAU) { rad -= TAU; } if (rad < 0) { rad += TAU; } player.radians[1] = rad; var sinY = Math.sin(-player.radians[1]); var cosY = Math.cos(-player.radians[1]); player.rotY[ 0] = cosY; player.rotY[ 2] = -sinY; player.rotY[ 8] = sinY; player.rotY[10] = cosY; } if (player.roll) { player.axisZ[0] = Math.sin(player.radians[0]); player.axisZ[1] = Math.sin(player.radians[1]); player.axisZ[2] = Math.cos(player.radians[2]); rad = player.radians[2] + mod * player.roll / 40; if (rad >= TAU) { rad -= TAU; } if (rad < 0) { rad += TAU; } player.radians[2] = rad; var sinZ = Math.sin(player.radians[2]); var cosZ = Math.cos(player.radians[2]); player.rotZ[ 0] = cosZ; player.rotZ[ 1] = -sinZ; player.rotZ[ 4] = sinZ; player.rotZ[ 5] = cosZ; } if (player.pitch) { player.axisX[0] = Math.cos(player.radians[0]); player.axisX[1] = Math.sin(player.radians[1]); player.axisX[2] = Math.sin(player.radians[2]); rad = player.radians[0] + mod * player.pitch / 40; if (rad >= TAU) { rad -= TAU; } if (rad < 0) { rad += TAU; } player.radians[0] = rad; var sinX = Math.sin(-player.radians[0]); var cosX = Math.cos(-player.radians[0]); player.rotX[ 5] = cosX; player.rotX[ 6] = sinX; player.rotX[ 9] = -sinX; player.rotX[10] = cosX; } } // Quaternion => [scalar, x-value * i, y-value * j, z-value * k] function transformQuat(v, q) { return [ q[3]*q[3]*v[0] + 2*q[1]*q[3]*v[2] - 2*q[2]*q[3]*v[1] + q[0]*q[0]*v[0] + 2*q[1]*q[0]*v[1] + 2*q[2]*q[0]*v[2] - q[2]*q[2]*v[0] - q[1]*q[1]*v[0], 2*q[0]*q[1]*v[0] + q[1]*q[1]*v[1] + 2*q[2]*q[1]*v[2] + 2*q[3]*q[2]*v[0] - q[2]*q[2]*v[1] + q[3]*q[3]*v[1] - 2*q[0]*q[3]*v[2] - q[0]*q[0]*v[1], 2*q[0]*q[2]*v[0] + 2*q[1]*q[2]*v[1] + q[2]*q[2]*v[2] - 2*q[3]*q[1]*v[0] - q[1]*q[1]*v[2] + 2*q[3]*q[0]*v[1] - q[0]*q[0]*v[2] + q[3]*q[3]*v[2], 1 // I set w to 1 because I don't need to scale anything ]; } /* quatX => Pitch quatY => Yaw quatZ => Roll */ function renderQuaternion() { var quatX = [ player.axisX[0] * Math.sin(player.radians[0] / 2), player.axisX[1] * Math.sin(player.radians[0] / 2), player.axisX[2] * Math.sin(player.radians[0] / 2), Math.cos(player.radians[0] / 2) ]; var quatY = [ player.axisY[0] * Math.sin(player.radians[1] / 2), player.axisY[1] * Math.sin(player.radians[1] / 2), player.axisY[2] * Math.sin(player.radians[1] / 2), Math.cos(player.radians[1] / 2) ]; var quatZ = [ player.axisZ[0] * Math.sin(player.radians[2] / 2), player.axisZ[1] * Math.sin(player.radians[2] / 2), player.axisZ[2] * Math.sin(player.radians[2] / 2), Math.cos(player.radians[2] / 2) ]; // Rendering happens here } 列作为分组器进行汇总。

答案 1 :(得分:1)

我提供了一个可以用数据框复制的示例。首先,我按5分钟的间隔创建一个虚拟时间序列(ts)as.POSIXct,然后使用dplyr将它们按15分钟间隔分组。

ts <- seq.POSIXt(as.POSIXct("2017-01-01", tz = "UTC"),
                 as.POSIXct("2017-02-01", tz = "UTC"),
                 by = "5 min")
ts <- as.data.frame(ts)
library(dplyr)
ts %>%
   group_by(interval = cut(ts, breaks = "15 min")) %>%
   summarise(count= n())

<强>输出

# A tibble: 2,977 x 2
   interval            sumvalue
   <fct>                  <int>
 1 2017-01-01 00:00:00        3
 2 2017-01-01 00:15:00        3
 3 2017-01-01 00:30:00        3
 4 2017-01-01 00:45:00        3
 5 2017-01-01 01:00:00        3
 6 2017-01-01 01:15:00        3
 7 2017-01-01 01:30:00        3
 8 2017-01-01 01:45:00        3
 9 2017-01-01 02:00:00        3
10 2017-01-01 02:15:00        3
# ... with 2,967 more rows