以R中等间隔的时间间隔聚合数据 [英] Aggregate data by equally spaced time intervals in R
问题描述
我的数据集是这样的
Section Time x
s3 9:35 2
s4 9:35 2
s1 9:36 1
s2 10:01 1
s8 11:00 2
所以,我想按小时间隔对数据节进行分组,并总结该间隔内的x值
So, I want to group the data section wise on hourly interval and sum up the x values that lies in that interval
我预期输出是
sec Time x
s1 9:00-10:00 1
s2 9:00-10:00 0
s3 9:00-10:00 2
s4 9:00-10:00 2
s8 9:00-10:00 0
s1 10.00-11.00 0
s2 10.00-11.00 1
s3 10.00-11.00 0
s4 10.00-11.00 0
s8 10.00-11.00 1
我试图从这篇文章中获得一些帮助堆栈溢出,但是我在此查询中遇到以下错误。 x是我的帧
I tried to get some help from this post in stack overflow, but I am getting the following error for my this query. Here x is my frame
data.frame(value = tapply(cbind(x$x),
list(sec= x$section,cut(x$Time, breaks="1 hour")),
sum))
Error in cut.default(x$Time, breaks = "1 hour") : 'x' must be numeric
我什至不确定这是对还是错。我从未在R中使用时间数据。因此,如何实现该目标的任何帮助将是巨大的帮助。
I am not even sure if that is right or wrong. I never worked with time data in R. So any help on how can I achieve that would be a great help.
推荐答案
I认为问题在于您的 Time
列采用字符格式吗?
I think the problem lies in the fact that your Time
column is in a character format ?
无论如何,这是一个使用dplyr的快速而肮脏的方法:
Anyway, here is a quick and dirty approach using dplyr :
library(dplyr)
x <- data.frame(section = c("s3", "s4", "s1", "s2", "s8", "s1", "s2", "s3"),
Time = c("9:35", "9:35", "9:36", "10:01", "11:00", "9:45", "10:05", "10:05"),
x = c(2, 2, 1, 1, 2, 6, 2, 4), stringsAsFactors = FALSE)
x %>%
rowwise %>%
mutate(aux = as.numeric(strsplit(Time, ":")[[1]][1]),
time = paste0(aux, ":00-", aux+1, ":00")) %>%
select(-aux, -Time) %>%
ungroup %>%
group_by(time, section) %>%
summarise(x = sum(x)) %>%
ungroup
这篇关于以R中等间隔的时间间隔聚合数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!