[英]Aggregate hourly data into monthly data starting with the yyyy-mm-dd h:m format in R
我一直在积极寻找我在R中的问题的解决方案,但没有找到任何解决我的问题的方法...
我有一份使用pepe memes数据提交的R报告,将于1月初提交。 我一直在研究pepe模因的价格,这是我的问题。 我的日期格式为yyyy-mm-dd h:m
,我想将这些日期汇总成月度数据。 我当时正在考虑先制作一个新文件,其时间戳格式为yyyy-mm
但我无法做到这一点。 转换为yyyy-mm-dd
格式时,我很成功,但是当我想转换为yyyy-mm-dd
格式时,我确实遇到了问题。
因此,更清楚地说,这是我的两个问题:
如何将我的yyyy-mm-dd h:m
日期与月度数据的平均值合计成月度日期(因此,格式为yyyy-mm
)?
如果您不知道如何直接汇总日期,那么你们中有谁知道如何从yyyy-mm-dd h:m
格式转换为yyyy-mm
一个吗?
这是我的数据集的一些行(只是一个摘要,它包含250多个行):
Timestamp ForwardQuantity TotalPriceUSDPerUnit
------------------------------------------------------------
1 2016-09-26 04:00:00 3 3.44
2 2016-09-26 04:00:00 7 3.44
3 2016-09-26 05:00:00 3 3.39
4 2016-09-26 05:00:00 1 3.39
5 2016-09-26 06:00:00 2 3.39
6 2016-09-26 13:00:00 4 2.84
7 2016-09-28 04:00:00 1 2.88
8 2016-09-28 04:00:00 1 2.92
9 2016-09-28 06:00:00 1 2.92
10 2016-09-28 06:00:00 1 2.92
在此先感谢大家,并为那些庆祝圣诞节的人们度过一个愉快的圣诞节!
编辑:预期结果:
Timestamp Average price
------------------------------------
1 2016-09 2.9981
在这里,平均价格是通过将上述远期数量乘以其相关价格而获得的
编辑2:dput(head(DatasHAIRPEPE3col,10))的输出如下
structure(list(Timestamp = structure(c(1474862400, 1474862400,
1474866000, 1474866000, 1474869600, 1474894800, 1475035200, 1475035200,
1475042400, 1475042400), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
ForwardQuantity = c(3L, 7L, 3L, 1L, 2L, 4L, 1L, 1L, 1L, 1L
), TotalPriceUSDPerUnit = c(3.445, 3.445, 3.392, 3.392, 3.392,
2.8352, 2.8795, 2.9238, 2.9238, 2.9238)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
最后使用注释中可重复显示的数据
1)动物园将数据转换为一个动物园对象,同时将其聚合到yearmon
类。 这将使动物园对象的Mean
为每年/每月一个平均值。 您可以使用它,也可以使用fortify.zoo将其转换为fortify.zoo
。 该解决方案可能比下面的(2)更方便,因为我们直接将year / month表示为yearmon
类对象,可以按逻辑方式对其进行绘制和操作。
library(zoo)
Mean <- read.zoo(DF, FUN = as.yearmon, aggregate = mean)
fortify.zoo(Mean) # optional
给这个数据框:
Index Mean
1 Sep 2016 3.406667
您现在可以进一步操作,例如使用plot.zoo
对其进行plot.zoo
如下所示:
plot(Mean)
2)基数R或者,使用每个时间戳的前7个字符表示年/月,并以此为基础进行汇总。
DF2 <- transform(DF, Timestamp = substring(Timestamp, 1, 7))
aggregate(UsdPricePerUnit ~ Timestamp, DF2, mean)
给予:
Timestamp UsdPricePerUnit
1 2016-09 3.406667
Lines <- "
Timestamp UsdPricePerUnit
2016-09-26 04:00:00 3.44
2016-09-26 04:00:00 3.44
2016-09-26 05:00:00 3.39
2016-09-26 05:00:00 3.39
2016-09-26 05:00:00 3.39
2016-09-26 06:00:00 3.39"
DF <- read.csv(textConnection(gsub(" +", ",", Lines)))
使用在前面的回答提供的样本数据连同(与附加月新增示范) dplyr
和anytime
library(tidyverse)
library(anytime)
Lines <- "
Timestamp ForwardQuantity UsdPricePerUnit
2016-09-26 04:00:00 3 3.44
2016-09-26 04:00:00 7 3.44
2016-09-26 05:00:00 3 3.39
2016-10-26 05:00:00 1 3.39
2016-10-26 05:00:00 2 3.39
2016-10-26 06:00:00 4 3.39"
DF <- read.csv(textConnection(gsub(" +", ",", Lines)))
DF %>%
mutate(month = format(anydate((Timestamp)), "%Y-%m")) %>%
group_by(month) %>%
mutate(MonthlySpend = ForwardQuantity*UsdPricePerUnit) %>%
summarise(QuanPerMon = sum(ForwardQuantity),
SpendPerMon = sum(MonthlySpend)) %>%
mutate(AveragePrice = SpendPerMon/QuanPerMon) %>%
select(1,4)
# A tibble: 2 x 2
month AveragePrice
<chr> <dbl>
1 2016-09 3.43
2 2016-10 3.39
编辑-新数据添加到问题
这对我来说对你的数据有用
df %>%
mutate(month = format(anydate((Timestamp)), "%Y-%m")) %>%
group_by(month) %>%
mutate(MonthlySpend = ForwardQuantity*TotalPriceUSDPerUnit) %>%
summarise(QuanPerMon = sum(ForwardQuantity),
SpendPerMon = sum(MonthlySpend)) %>%
mutate(AveragePrice = SpendPerMon/QuanPerMon) %>%
select(1,4)
# A tibble: 1 x 2
month AveragePrice
<chr> <dbl>
1 2016-09 3.24
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.