R:5分钟间隔添加到数据帧
对意图的快速概述R:5分钟间隔添加到数据帧,r,aggregate,intervals,R,Aggregate,Intervals,对意图的快速概述 传感器:触发传感器时的发生时间和房间(pings) 时隙:每个房间30分钟的结构化时间窗口,用于将数据排序到 查找每个房间每个5分钟区块中的ping数量。结构转换为所需的表 注意: -每个30分钟的时间段(0-5分钟、5-10分钟、10-15分钟等)有6组5分钟窗口 -每个房间在ping计数方面与其他房间相同 -这是一个更大的装置的小版本(更多的房间,更多的30分钟街区) -如果值为上午10:05:00,则属于括号“0到5分钟”,而不是“5-10”。 -10.05:01属于
- 传感器:触发传感器时的发生时间和房间(pings)
- 时隙:每个房间30分钟的结构化时间窗口,用于将数据排序到 查找每个房间每个5分钟区块中的ping数量。结构转换为所需的表
>dput(timeslot)
structure(list(Room = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Alpha",
"Beta", "Gamma"), class = "factor"), Capacity = c(16L, 16L, 16L,
16L, 10L, 10L), Video.Conference = c(1L, 1L, 1L, 1L, 0L, 0L),
ST = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("2014-12-22 10:00:00",
"2014-12-22 10:30:00"), class = "factor"), ET = structure(c(1L,
2L, 1L, 2L, 1L, 2L), .Label = c("2014-12-22 10:30:00", "2014-12-22 11:00:00"
), class = "factor")), .Names = c("Room", "Capacity", "Video.Conference",
"ST", "ET"), row.names = c(NA, -6L), class = "data.frame")
> dput(sensor)
structure(list(UTC = structure(c(4L, 5L, 6L, 7L, 9L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 20L, 1L, 3L, 6L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 18L, 19L, 21L, 2L, 22L), .Label = c("2014-12-22 10:08:34",
"2014-12-22 10:09:34", "2014-12-22 10:14:00", "2014-12-22 10:14:34",
"2014-12-22 10:15:00", "2014-12-22 10:15:25", "2014-12-22 10:15:27",
"2014-12-22 10:15:29", "2014-12-22 10:16:00", "2014-12-22 10:17:00",
"2014-12-22 10:18:00", "2014-12-22 10:18:03", "2014-12-22 10:18:10",
"2014-12-22 10:18:39", "2014-12-22 10:19:00", "2014-12-22 10:19:04",
"2014-12-22 10:19:06", "2014-12-22 10:19:09", "2014-12-22 10:19:11",
"2014-12-22 10:20:00", "2014-12-22 10:20:20", "2014-12-22 10:39:11"
), class = "factor"), pin = c(1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L,
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L,
0L, 1L, 0L, 1L, 1L), Room = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("Alpha", "Beta", "Gamma"
), class = "factor")), .Names = c("UTC", "pin", "Room"), row.names = c(NA,
-29L), class = "data.frame")
这里有一个方法。我相信有更好的方法来做这项工作。但是,这可能是您可以为更大的数据集进一步安排的内容
library(dplyr) # I use the dev version, dplyr 0.4
library(tidyr)
library(chron)
### Arrange sensor
sensor <- mutate(sensor, UTC = as.POSIXct(UTC, format = "%Y-%m-%d %H:%M:%S"),
minute = as.numeric(format(UTC, "%M")),
minute = ifelse(minute >= 30, minute-30, minute),
sub.group = as.character(cut(minute,
breaks = c(0, 5, 10, 15, 20, 25, 30),
labels = c("0-5", "5-10", "10-15", "15-20", "20-25", "25-30"))),
time = times(as.character(format(UTC, "%H:%M:%S"))),
time = as.character(cut(time,
breaks = times(c("10:00:00", "10:30:00", "11:00:00")),
labels = c("a", "b"))),
Room = as.character(Room))
### Create a master df
master <- data.frame(Room = rep(c("Alpha", "Beta", "Gamma"), each = 6),
time = rep(c("a", "b"), each = 18),
sub.group = rep(c("0-5", "5-10", "10-15",
"15-20", "20-25", "25-30"), times = 3),
stringsAsFactors = FALSE)
### Get an outcome
df1 <- full_join(summarise(group_by(sensor, Room,time, sub.group), total = sum(pin)),
master, by = c("Room","time", "sub.group")) %>%
spread(sub.group, total, fill = 0)
### Arrange timeslot for join
timeslot <- mutate_each(timeslot,funs(as.POSIXct(., format = "%Y-%m-%d %H:%M:%S")), ST:ET) %>%
mutate(time = as.character(format(ST, "%M")),
time = ifelse(time == "00", "a", "b"),
Room = as.character(Room))
### join again
final <- full_join(timeslot, df1, by = c("Room", "time")) %>%
select(-time)
### Order columns
final <- final[, c(1:6, 11, 7:10)]
### Get row sum
mutate(final, total = rowSums(final[, c(6:11)]))
# Room Capacity Video.Conference ST ET 0-5 5-10 10-15 15-20 20-25 25-30 total
#1 Alpha 16 1 2014-12-22 10:00:00 2014-12-22 10:30:00 0 0 2 4 0 0 6
#2 Alpha 16 1 2014-12-22 10:30:00 2014-12-22 11:00:00 0 0 0 0 0 0 0
#3 Beta 16 1 2014-12-22 10:00:00 2014-12-22 10:30:00 0 1 1 5 0 0 7
#4 Beta 16 1 2014-12-22 10:30:00 2014-12-22 11:00:00 0 0 0 0 0 0 0
#5 Gamma 10 0 2014-12-22 10:00:00 2014-12-22 10:30:00 0 1 0 0 0 0 1
#6 Gamma 10 0 2014-12-22 10:30:00 2014-12-22 11:00:00 0 1 0 0 0 0 1
library(dplyr)#我使用开发版本dplyr 0.4
图书馆(tidyr)
图书馆(计时)
###排列传感器
传感器=30,分钟-30,分钟),
子组=作为字符(切割(分钟),
断裂=c(0,5,10,15,20,25,30),
标签=c(“0-5”、“5-10”、“10-15”、“15-20”、“20-25”、“25-30”),
时间=时间(以.character格式(UTC,%H:%M:%S)),
时间=作为字符(剪切(时间,
休息时间=时间(c(“10:00:00”、“10:30:00”、“11:00:00”),
标签=c(“a”、“b”),
房间=作为角色(房间))
###创建主df
主人
library(dplyr) # I use the dev version, dplyr 0.4
library(tidyr)
library(chron)
### Arrange sensor
sensor <- mutate(sensor, UTC = as.POSIXct(UTC, format = "%Y-%m-%d %H:%M:%S"),
minute = as.numeric(format(UTC, "%M")),
minute = ifelse(minute >= 30, minute-30, minute),
sub.group = as.character(cut(minute,
breaks = c(0, 5, 10, 15, 20, 25, 30),
labels = c("0-5", "5-10", "10-15", "15-20", "20-25", "25-30"))),
time = times(as.character(format(UTC, "%H:%M:%S"))),
time = as.character(cut(time,
breaks = times(c("10:00:00", "10:30:00", "11:00:00")),
labels = c("a", "b"))),
Room = as.character(Room))
### Create a master df
master <- data.frame(Room = rep(c("Alpha", "Beta", "Gamma"), each = 6),
time = rep(c("a", "b"), each = 18),
sub.group = rep(c("0-5", "5-10", "10-15",
"15-20", "20-25", "25-30"), times = 3),
stringsAsFactors = FALSE)
### Get an outcome
df1 <- full_join(summarise(group_by(sensor, Room,time, sub.group), total = sum(pin)),
master, by = c("Room","time", "sub.group")) %>%
spread(sub.group, total, fill = 0)
### Arrange timeslot for join
timeslot <- mutate_each(timeslot,funs(as.POSIXct(., format = "%Y-%m-%d %H:%M:%S")), ST:ET) %>%
mutate(time = as.character(format(ST, "%M")),
time = ifelse(time == "00", "a", "b"),
Room = as.character(Room))
### join again
final <- full_join(timeslot, df1, by = c("Room", "time")) %>%
select(-time)
### Order columns
final <- final[, c(1:6, 11, 7:10)]
### Get row sum
mutate(final, total = rowSums(final[, c(6:11)]))
# Room Capacity Video.Conference ST ET 0-5 5-10 10-15 15-20 20-25 25-30 total
#1 Alpha 16 1 2014-12-22 10:00:00 2014-12-22 10:30:00 0 0 2 4 0 0 6
#2 Alpha 16 1 2014-12-22 10:30:00 2014-12-22 11:00:00 0 0 0 0 0 0 0
#3 Beta 16 1 2014-12-22 10:00:00 2014-12-22 10:30:00 0 1 1 5 0 0 7
#4 Beta 16 1 2014-12-22 10:30:00 2014-12-22 11:00:00 0 0 0 0 0 0 0
#5 Gamma 10 0 2014-12-22 10:00:00 2014-12-22 10:30:00 0 1 0 0 0 0 1
#6 Gamma 10 0 2014-12-22 10:30:00 2014-12-22 11:00:00 0 1 0 0 0 0 1