简体   繁体   English

如何在时间序列数据框中获取汇总表/箱线图?

[英]How can get summary table/boxplot in time sequence data frame?

I have a data frame which contains time sequence, like this:我有一个包含时间序列的数据框,如下所示:

example <- data.frame(
Date=seq(
 from=as.POSIXct("2012-1-1 0:00", tz="UTC"),
 to=as.POSIXct("2012-1-31 23:00", tz="UTC"),
 by="10 min"),
 frequency=runif(4459, min=12, max=26))

I would like count min value, mean, max value etc. (using summary table) by days: for example summary table of days 2012 1. 1. (using only the first 144 raws), 2012 1. 2. (using raws from 145 to 288), 2012 1. 3. (using raws from 289 to 432) etc.我想按天计算最小值、平均值、最大值等(使用汇总表):例如 2012 1. 1. 天的汇总表(仅使用前 144 个原始数据),2012 1. 2.(使用来自的原始数据) 145 至 288),2012 年 1. 3.(使用 289 至 432 的原始数据)等。

how can I get this table?我怎样才能得到这张桌子? I have tried this我试过这个

summary(example$freqency, example$Date, by="day")

how can I draw dropbox for every day separately?如何分别为每一天绘制保管箱? I have tried this:我试过这个:

boxplot(example$freqency, example$Date, by="day")

How can I select time data within days?如何选择几天内的时间数据? I also want to calculate summary table by days, but in this case I want to use only data in every hours (eg 0:00, 1:00, 2:00 etc.)我也想按天计算汇总表,但在这种情况下,我只想使用每小时的数据(例如 0:00、1:00、2:00 等)

Can somebody help me?有人可以帮助我吗?

To get summary of frequency by day, you could use aggregate from base R in combination with strftime() :要按天获取frequency摘要,您可以将base R 中的aggregatestrftime()结合使用:

aggregate(frequency ~ strftime(Date, "%d"),
          FUN = summary, data = example)

To get a boxplot per day, we just need to create a $day column for the x-axis in ggplot2 .要获得每天的箱线图,我们只需要为ggplot2x-axis创建一个$day列。

library(ggplot2)
example$day <- strftime(example$Date, "%d")
ggplot(example, aes(x = factor(day), y = frequency)) + geom_boxplot()

在此处输入图片说明

Try this simply:简单地试试这个:

within days:几天内:

example$str.date <- substring(as.character(example$Date),1,10)
summary.example <- aggregate(frequency~str.date, example, FUN = summary)
library(ggplot2)
ggplot(example, aes(str.date, frequency, group=str.date, fill=str.date)) + geom_boxplot()  + 
  theme(axis.text.x = element_text(angle=90, vjust = 0.5))

在此处输入图片说明

within hours (within each day):小时内(每天):

example$str.date.hrs <- substring(as.character(example$Date),1,13)
summary.example <- aggregate(frequency~str.date.hrs, example, FUN = summary)
library(ggplot2)
ggplot(example[example$str.date=='2012-01-01',], aes(str.date.hrs, frequency, group=str.date.hrs, fill=str.date.hrs)) + geom_boxplot()  + 
  theme(axis.text.x = element_text(angle=90, vjust = 0.5))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM