0

我手头的任务是对时间序列数据进行异常检测。我有异常检测代码,但正在尝试为其准备数据。数据如下所示。

timestampUtc

2016-08-01 14:38:01, 2016-08-01 14:38:06, 2016-08-01 14:38:12, 2016-08-01 14:38:18, 2016-08-01 14:38:22, 2016-08-01 14:38:27, 2016-08-01 14:38:27, 2016-08-01 14:38:30, 2016-08-01 14:38:37, 2016-08-01 14:38:38, 2016-08-01 14:38:38, 2016-08-01 14:38:46, 2016-08-01 14:39:03, 2016-08-01 14:39:03, 2016-08-01 14:39:10, 2016-08-01 14:39:12, 2016-08-01 14:39:15, 2016-08-01 14:39:16, 2016-08-01 14:39:20, 2016-08-01 14:39:28

首先,我想让timestampUtc列中的秒数为零。接下来,我想创建一个列,表示count并想计算该特定分钟的值的数量。例如,输出应如下所示:

timestampUtc count

2016-08-01 14:38:00 12, 2016-08-01 14:39:00 6, 2016-08-01 14:40:00 8
4

3 回答 3

1

假设您的时间戳已经是 POSIXt 格式,并且您的时间戳数据存储在 df-

df$count <- 1
df$timestamp <- format(df$timestamp, format = "%Y-%m-%d %H:%M")
df <- aggregate(count ~ timestamp, data = df, FUN = sum)
names(df) <- c("timestamp", "count")
于 2016-11-21T20:07:36.590 回答
1

POSIXt 类的 thecutseq方法都具有breaks(or by) 的区间选项:

 timestampUtc <-scan(text="2016-08-01 14:38:01, 2016-08-01 14:38:06, 2016-08-01 14:38:12, 2016-08-01 14:38:18, 2016-08-01 14:38:22, 2016-08-01 14:38:27, 2016-08-01 14:38:27, 2016-08-01 14:38:30, 2016-08-01 14:38:37, 2016-08-01 14:38:38, 2016-08-01 14:38:38, 2016-08-01 14:38:46, 2016-08-01 14:39:03, 2016-08-01 14:39:03, 2016-08-01 14:39:10, 2016-08-01 14:39:12, 2016-08-01 14:39:15, 2016-08-01 14:39:16, 2016-08-01 14:39:20, 2016-08-01 14:39:28",
                      what="", sep=",")
#Read 20 items

table( cut( as.POSIXct(timestampUtc), breaks="min")  )
#------------
2016-08-01 14:38:00 2016-08-01 14:39:00 
                 12                   8 

如果您想要 10 或 15 分钟的间隔,它可能是“10 分钟”或“15 分钟”。到目前为止,其他答案之一删除了输入阶段的信息,我认为这是一种有问题的做法,但 code_is_entropyformat在传递到的阶段与缩短的格式字符串一起使用table

于 2016-11-21T20:38:09.183 回答
1

您可以使用as.POSIXct()将字符串转换为日期,使用某种格式忽略秒,然后使用table总结:

timestampUtc <- c('2016-08-01 14:38:01', '2016-08-01 14:38:06', '2016-08-01 14:38:12', '2016-08-01 14:38:18', '2016-08-01 14:38:22', '2016-08-01 14:38:27', '2016-08-01 14:38:27', '2016-08-01 14:38:30', '2016-08-01 14:38:37', '2016-08-01 14:38:38', '2016-08-01 14:38:38', '2016-08-01 14:38:46', '2016-08-01 14:39:03', '2016-08-01 14:39:03', '2016-08-01 14:39:10', '2016-08-01 14:39:12', '2016-08-01 14:39:15', '2016-08-01 14:39:16', '2016-08-01 14:39:20', '2016-08-01 14:39:28')
timestampUtc <- as.POSIXct(timestampUtc, format="%Y-%m-%d %H:%M", tz="UTC")
table(timestampUtc)
2016-08-01 14:38:00 2016-08-01 14:39:00 
                 12                   8 
于 2016-11-21T20:06:27.437 回答