简体   繁体   English

是否有 R function 来汇总时间序列值

[英]Is there an R function to summarise time-series values

I have a question that I can't answer myself even after searching for a while and for this reason I am hoping for a hint.我有一个问题,即使搜索了一段时间我也无法回答自己,因此我希望得到提示。

My challenge is that I have a time series that provides various other collected values as elements.我的挑战是我有一个时间序列,它提供各种其他收集的值作为元素。 The timestamps are stored in a column.时间戳存储在列中。 In the other columns are the other values.在其他列中是其他值。 The values are provided on a daily basis.这些值每天提供。 Since I want to compare the values with another time series whose values are provided on a monthly basis, I want to summarize the values of the first table to average monthly values.由于我想将这些值与另一个按月提供值的时间序列进行比较,因此我想总结第一个表的值以求平均月值。 I don't care if the values are added back to the original table or if a new table is created.我不在乎这些值是被添加回原始表还是创建了一个新表。

Many thanks in advance.提前谢谢了。

Update:更新:

Unfortunately, the data is company data, so I cannot publish it.不幸的是,数据是公司数据,所以我无法发布。 But maybe an example will help:但也许一个例子会有所帮助:

Timestamp时间戳 Department部门 Value1值 1 Value2值2
1970-01-01 09:00:00 1970-01-01 09:00:00 Procurement采购 0.4 0.4 0.9 0.9
1970-01-01 09:00:00 1970-01-01 09:00:00 R&D研发 0.2 0.2 0.2 0.2
1970-01-01 09:00:00 1970-01-01 09:00:00 IT 0.6 0.6 NA北美
1970-01-02 09:00:00 1970-01-02 09:00:00 Procurement采购 0.1 0.1 0.2 0.2
1970-01-02 09:00:00 1970-01-02 09:00:00 R&D研发 NA北美 0.3 0.3
1970-01-02 09:00:00 1970-01-02 09:00:00 IT 0.9 0.9 0.5 0.5
... ... ... ... ... ... ... ...

You can create a month variable, and then group by and summarise the values您可以创建一个月份变量,然后分组汇总

df %>% 
  mutate(month = month(Timestamp)) %>% 
  group_by(month) %>% 
  summarise(
    across(.cols = c(Value1,Value2),.fns = ~mean(.,na.rm = T))
  )

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

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