按不规则时间间隔对数据进行分组和求和(R语言) [英] Grouping and Summing Data by Irregular Time Intervals (R language)

查看:256
本文介绍了按不规则时间间隔对数据进行分组和求和(R语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此处的stackoverflow帖子: R:计数一组内的观察数

I am looking at a stackoverflow post over here: R: Count Number of Observations within a group

在这里,每天创建数据,并按月间隔(以及每周间隔)进行汇总/分组:

Here, daily data is created and summed/grouped at monthly intervals (as well as weekly intervals):

library(xts)
library(dplyr)

#create data

date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")

property_damages_in_dollars <- rnorm(731,100,10)

final_data <- data.frame(date_decision_made, property_damages_in_dollars)


# weekly

weekly = final_data %>%
    mutate(date_decision_made = as.Date(date_decision_made)) %>%
    group_by(week = format(date_decision_made, "%W-%y")) %>%
    summarise( total = sum(property_damages_in_dollars, na.rm = TRUE), Count = n())


# monthly 

final_data %>%
    mutate(date_decision_made = as.Date(date_decision_made)) %>%
    group_by(week = format(date_decision_made, "%Y-%m")) %>%
    summarise( total = sum(property_damages_in_dollars, na.rm = TRUE), Count = n())

似乎格式"R中的语句( https://www.rdocumentation.org/package/base/versions/3.6.2/topics/format )用于指示计算机对分组和求和"进行分组.数据间隔一定的时间.

It seems that the "format" statement in R (https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/format) is being used to instruct the computer to "group and sum" the data some fixed interval.

我的问题:有没有一种方法可以指导"某人?计算机将其分组和求和".不定期地间隔?例如.是11天,3个月还是2年?(我想3个月可以写成90天... 2年可以写成730天).

My question: is there a way to "instruct" the computer to "group and sum" by irregular intervals? E.g. by 11 day periods, by 3 month periods, by 2 year periods? (I guess 3 months can be written as 90 days...2 years can be written as 730 days).

这可能吗?

谢谢

推荐答案

您可以使用lubridate的 ceiling_date / floor_date 定期创建组.

You can use lubridate's ceiling_date/floor_date to create groups at irregular intervals.

library(dplyr)
library(lubridate)

final_data %>%
  mutate(date_decision_made = as.Date(date_decision_made)) %>%
  group_by(group = ceiling_date(date_decision_made, '11 days')) %>%
  summarise(amount = sum(property_damages_in_dollars))

您还可以指定间隔,例如 ceiling_date(date_decision_made,'3 years') ceiling_date(date_decision_made,'2 months').

You can also specify intervals like ceiling_date(date_decision_made, '3 years') or ceiling_date(date_decision_made, '2 months').

这篇关于按不规则时间间隔对数据进行分组和求和(R语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆