如何在R中以Year/Semester格式创建日期? [英] How can I create dates in Year/Semester format in R?

查看:172
本文介绍了如何在R中以Year/Semester格式创建日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个,四个或六个月的时间内汇总R中的动物园数据.对于这种类型的日期处理,只有两个可用的选项,使用:

I want to aggregate zoo data in R by two, four or six months periods. There are only two avaliable options for this type of date processing, using:

a)as.yearmon =>处理按每月分组的每日数据

a) as.yearmon => process daily data grouped by each month

b)as.yearqtr =>按固定的三个月(3月,3月,4月,7月和10月)的分组对每日数据进行处理.

b) as.yearqtr => process daily data grouped by fixed groups of 3 months periods (jan-mar, apr-jun, jul-set and oct-dec).

library(zoo)        
# creating a vector of Dates 
dt = as.Date(c("2001-01-01","2001-01-02","2001-04-01","2001-05-01","2001-07-01","2001-10-01"),
             "%Y-%m-%d")
# the original dates        
dt
[1] "2001-01-01" "2001-01-02" "2001-04-01" "2001-05-01" "2001-07-01" "2001-10-01"

# conversion to monthly data
as.yearmon(dt)
[1] "jan 2001" "jan 2001" "abr 2001" "mai 2001" "jul 2001" "out 2001"

# conversion to quarterly data
as.yearqtr(dt)
[1] "2001 Q1" "2001 Q1" "2001 Q2" "2001 Q2" "2001 Q3" "2001 Q4"

set.seed(0)
# irregular time series
daily_db = zoo(matrix(rnorm(3 * length(dt)),
                    nrow = length(dt),
                    ncol = 3),
             order.by = dt)
daily_db                                                
2001-01-01  1.2629543 -0.928567035 -1.1476570
2001-01-02 -0.3262334 -0.294720447 -0.2894616
2001-04-01  1.3297993 -0.005767173 -0.2992151
2001-05-01  1.2724293  2.404653389 -0.4115108
2001-07-01  0.4146414  0.763593461  0.2522234
2001-10-01 -1.5399500 -0.799009249 -0.8919211

# data aggregated by month
aggregate(daily_db,as.yearmon,sum)
                 V1           V2         V3
jan 2001  0.9367209 -1.223287482 -1.4371186
abr 2001  1.3297993 -0.005767173 -0.2992151
mai 2001  1.2724293  2.404653389 -0.4115108
jul 2001  0.4146414  0.763593461  0.2522234
out 2001 -1.5399500 -0.799009249 -0.8919211

# data aggregated by quarter
aggregate(daily_db,as.yearqtr,sum)
                V1         V2         V3
2001 Q1  0.9367209 -1.2232875 -1.4371186
2001 Q2  2.6022286  2.3988862 -0.7107260
2001 Q3  0.4146414  0.7635935  0.2522234
2001 Q4 -1.5399500 -0.7990092 -0.8919211

我想定义一个函数,例如:

I want to define a function like:

as.yearperiod = function(x, period = 6) {...} # convert dates in semesters

使用这种方式:

# data aggregated by semester
aggregate(base_dados_diaria, as.yearperiod, period = 6, sum)

我希望得到这样的结果:

I expect an result like this one:

                V1         V2         V3
2001 S1  3.538950   1.175599  -2.147845
2001 S2 -1.125309  -0.035416  -0.639698

推荐答案

先生,我建议您使用 lubridate 包,以处理自定义日期间隔.应用下一个日期,如下所示:

Sir, I suggest you to use lubridate package, to deal with custom date intervals. Your task could be easy accomplished applying floor_date, as below:

six_m_interval <- lubridate::floor_date( dt , "6 months" )
# [1] "2001-01-01" "2001-01-01" "2001-01-01" "2001-01-01" "2001-07-01" "2001-07-01"

aggregate( daily_db , six_m_interval , sum )
#                  V1          V2         V3
# 2001-01-01  3.538950  1.17559873 -2.1478445
# 2001-07-01 -1.125309 -0.03541579 -0.6396977

这篇关于如何在R中以Year/Semester格式创建日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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