如何在[R]中按天划分值

时间:2018-01-09 16:51:42

标签: r date syntax days

我有这样的数据。

spot3new<- aggregate(spot3$Price~spot3$Date, spot3, sum)


head(spot3new)
spot3$Date    spot3$Price
1 2011-01-01      -16.32
2 2011-01-02      861.11
3 2011-01-03     1381.69
4 2011-01-04     1497.08
5 2011-01-05     1228.44
6 2011-01-06     1226.26

在这里,我按日期汇总每小时的价格。所以24小时的价格总结在一起。现在我想把它们除以平均价格的小时数。 所以我这样做了。

spot3newD<- data.frame(spot3new$`spot3$Date`, spot3new$`spot3$Price`/24)

> head(spot3newD)
     spot3new..spot3.Date.    spot3new..spot3.Price..24
1            2011-01-01                  -0.68000
2            2011-01-02                  35.87958
3            2011-01-03                  57.57042
4            2011-01-04                  62.37833
5            2011-01-05                  51.18500
6            2011-01-06                  51.09417

但我意识到,在48000次观察中,有一些缺失值。因此,如果我除以24,它将不会产生“每日平均价格”,因为从我的数据中间某处,价格将因缺少价值而被拉或推。 所以我不想分割我的数据by 24。我希望每天从2015-12-31到2011-01-02划分by date or days,这样他们就不会有任何错误估计。

任何人都可以帮我解决语法问题吗?

非常感谢你。

1 个答案:

答案 0 :(得分:0)

如果你想计算每日均值,你可以使用聚合函数,只要你有日期格式的日期。

dailymean <- aggregate(Price ~ day(Date), data, mean, na.rm=T)
来自'lubridate'包裹的

'day'功能