我一直在积极寻找R中问题的解决方案,但没有找到任何解决问题的方法。。。
我有一份R报告要在一月初提交,使用pepe模因数据。我一直在研究pepe模因的价格,我的问题来了。我有格式为yyyy-mm-dd h:m
的日期,我想将这些日期聚合为每月数据。我想先制作一个新文件,使用yyyy-mm
格式的时间戳,但我做不到。我在转换为yyyy-mm-dd
格式时很成功,但当我想转换为yyyy-mm格式时,我确实遇到了问题。
所以,更清楚地说,这是我的两个问题:
-
如何将我的
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(头(DatashairEPE3col,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)zoo将数据转换为zoo对象,同时将其聚合为类yearmon
。这将使动物园对象Mean
具有每年/每月一次的平均值。您可以使用它,也可以使用fortify.zoo
将其转换为数据帧。这个解决方案可能比下面的(2)更方便,因为我们直接将年/月表示为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(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