Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中创建年/学期格式的日期?_R_Date_Zoo_As.yearqtr - Fatal编程技术网

如何在R中创建年/学期格式的日期?

如何在R中创建年/学期格式的日期?,r,date,zoo,as.yearqtr,R,Date,Zoo,As.yearqtr,我想将R中的动物园数据按两个月、四个月或六个月的周期进行汇总。对于这种类型的日期处理,只有两个可用选项,使用: a) as.yearmon=>处理每月分组的每日数据 b) as.yearqtr=>处理按3个月固定组分组的每日数据(1-3月、4-6月、7月和10-12月) 最简单的例子 我想定义一个函数,如: as.yearperiod = function(x, period = 6) {...} # convert dates in semesters 要使用这种方式: # data agg

我想将R中的动物园数据按两个月、四个月或六个月的周期进行汇总。对于这种类型的日期处理,只有两个可用选项,使用:

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

b)
as.yearqtr
=>处理按3个月固定组分组的每日数据(1-3月、4-6月、7月和10-12月)

最简单的例子 我想定义一个函数,如:

as.yearperiod = function(x, period = 6) {...} # convert dates in semesters
要使用这种方式:

# data aggregated by semester
aggregate(base_dados_diaria, as.yearperiod, period = 6, sum)
我期待这样的结果:

                V1         V2         V3
2001 S1  3.538950   1.175599  -2.147845
2001 S2 -1.125309  -0.035416  -0.639698
先生,我建议您使用软件包,来处理定制的日期间隔。您的任务可以通过以下方式轻松完成:

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
six_m_interval Date2 Period
Date2period
输入一个
“Date”
对象,并根据参数
period
的值返回一个表示周期(学期等)的字符串,该值应为12的除数。在内部,它转换为
yearmon
,然后提取年份和周期,即月份,并从中生成所需字符串

Date2period <- function(x, period = 6, sep = " S") {
  ym <- as.yearmon(x)
  paste(as.integer(ym), (cycle(ym) - 1) %/% period + 1, sep = sep)
}

通过创建S3对象,例如
yearmon
,这可能会变得更加复杂,但问题中所示的目的并不是真正需要的。

您有问题吗?看起来你没有,有些不对劲。你的数据,在几个月内汇总,在几个季度内汇总,都是相同的数据!!我想你应该再检查一下上面的数据。在矩阵中,你说的是
nrow=length(dt)
length(dt)=4
但是你得到了一个有5行的矩阵。。怎么做???我在Onyanbu修复了dt对象。谢谢。关于我的英语@InfiniteFlashChess很抱歉。我将更改标题。
Date2period <- function(x, period = 6, sep = " S") {
  ym <- as.yearmon(x)
  paste(as.integer(ym), (cycle(ym) - 1) %/% period + 1, sep = sep)
}
library(zoo)

# inputs
period <- 6
dt <- as.Date(c("2001-01-01","2001-04-01","2001-07-01","2001-10-01"))

Date2period(dt)
## [1] "2001 S1" "2001 S1" "2001 S2" "2001 S2"

aggregate(daily_db, Date2period, sum)
##                V1        V2          V3
## 2001 S1 0.9367209 -1.125309  2.39888622
## 2001 S2 2.6022286 -1.223287 -0.03541579
period2yearmon <- function(x, period = 6) {
     year <- as.numeric(sub("\\D.*", "", x))
     cyc <- as.numeric(sub(".*\\D", "", x))
     as.yearmon(year + period * (cyc - 1) / 12)
}

period2Date <- function(x, period = 6) as.Date(period2yearmon(x, period))
# create a period string
d <- Date2period(dt)
## [1] "2001 S1" "2001 S1" "2001 S2" "2001 S2"

period2yearmon(d)
## [1] "Jan 2001" "Jan 2001" "Jul 2001" "Jul 2001"

period2Date(d)
## [1] "2001-01-01" "2001-01-01" "2001-07-01" "2001-07-01"

aggregate(daily_db, function(x) period2Date(Date2period(x)), sum)
##                   V1        V2          V3
## 2001-01-01 0.9367209 -1.125309  2.39888622
## 2001-07-01 2.6022286 -1.223287 -0.03541579