aggregate.zoo(zoo)
aggregate.zoo()所属R语言包:zoo
Compute Summary Statistics of zoo Objects
动物园的对象,计算汇总统计数据
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Splits a "zoo" object into subsets along a coarser index grid, computes summary statistics for each, and returns the reduced "zoo" object.
一个"zoo"对象分割为子集,沿着粗糙的指数网格,计算汇总统计数据,每个返回减少"zoo"对象。
用法----------Usage----------
## S3 method for class 'zoo'
aggregate(x, by, FUN = sum, ..., regular = NULL, frequency = NULL)
参数----------Arguments----------
参数:x
an object of class "zoo".
对象类"zoo"。
参数:by
index vector of the same length as index(x) which defines aggregation groups and the new index to be associated with each group. If by is a function, then it is applied to index(x) to obtain the aggregation groups.
相同的长度的索引向量index(x)汇聚组和要与每个组相关联的新的索引定义。如果by是一个函数,那么它被应用到index(x)获得的汇聚组。
参数:FUN
a scalar function to compute the summary statistics which can be applied to all subsets.
一个标量函数来计算的汇总统计数据,可以应用到所有的子集。
参数:...
further arguments passed to FUN.
进一步的参数传递给FUN。
参数:regular
logical. Should the aggregated series be coerced to class "zooreg" (if the series is regular)? The default is FALSE for "zoo" series and TRUE for "zooreg" series.
逻辑。被强制转换为类的聚合系列"zooreg"(如果该系列产品是正规的)?默认值是FALSE"zoo"系列和TRUE"zooreg"系列。
参数:frequency
numeric indicating the frequency of the aggregated series (if a "zooreg" series should be returned. The default is to determine the frequency from the data if regular is TRUE. If frequency is specified, it sets regular to TRUE. See examples for illustration.
数字显示频率的聚合系列(应予以返回。如果"zooreg"系列的默认频率来确定的数据,如果regular是TRUE。如果frequency被指定,它设置regular到TRUE。举例说明。
值----------Value----------
An object of class "zoo" or "zooreg".
对象的类"zoo"或"zooreg"。
注意----------Note----------
The xts package functions endpoints, period.apply to.period, to.weekly, to.monthly, etc., can also directly input and output certain zoo objects and
xts包功能endpoints,period.applyto.period,to.weekly,to.monthly“等,也可以直接输入和输出的某些<X >对象和
参见----------See Also----------
zoo
zoo
实例----------Examples----------
## averaging over values in a month:[#平均在一个月内的值:]
# x.date is jan 1,3,5,7; feb 9,11,13; mar 15,17,19[x.date月1,3,5,7月9,11,13月15,17,19]
x.date <- as.Date(paste(2004, rep(1:4, 4:1), seq(1,20,2), sep = "-")); x.date
x <- zoo(rnorm(12), x.date); x
# coarser dates - jan 1 (4 times), feb 1 (3 times), mar 1 (3 times)[粗糙的日期 - 1月1日(4次),2月1日(3次),3月1日(3次)]
x.date2 <- as.Date(paste(2004, rep(1:4, 4:1), 1, sep = "-")); x.date2
x2 <- aggregate(x, x.date2, mean); x2
# same - uses as.yearmon[相同的 - 使用as.yearmon]
x2a <- aggregate(x, as.Date(as.yearmon(time(x))), mean); x2a
# same - uses by function[相同的 - 使用的功能]
x2b <- aggregate(x, function(tt) as.Date(as.yearmon(tt)), mean); x2b
# same - uses cut[一样的 - 采用切]
x2c <- aggregate(x, as.Date(cut(time(x), "month")), mean); x2c
# almost same but times of x2d have yearmon class rather than Date class[几乎是相同的,但时间x2d有yearmon类,而不是Date类]
x2d <- aggregate(x, as.yearmon, mean); x2d
# compare time series[时间序列进行比较]
plot(x)
lines(x2, col = 2)
## aggregate a daily time series to a quarterly series[#汇总每天的时间序列的季度系列]
# create zoo series[动物园系列]
tt <- as.Date("2000-1-1") + 0:300
z.day <- zoo(0:300, tt)
# function which returns corresponding first "Date" of quarter[函数返回对应的第一季度的“日期”]
first.of.quarter <- function(tt) as.Date(as.yearqtr(tt))
# average z over quarters[超过四分之三的平均Z]
# 1. via "yearqtr" index (regular)[1。通过“yearqtr”的指数(常规)]
# 2. via "Date" index (not regular)[2。通过“日期”指数(不经常)]
z.qtr1 <- aggregate(z.day, as.yearqtr, mean)
z.qtr2 <- aggregate(z.day, first.of.quarter, mean)
# The last one used the first day of the quarter but suppose[最后一个使用了本季度的第一天,但假设]
# we want the first day of the quarter that exists in the series[我们希望本季度的第一天中存在的一系列]
# (and the series does not necessarily start on the first day[(和系列不必然的第一天开始]
# of the quarter).[季度)。]
z.day[!duplicated(as.yearqtr(time(z.day)))]
# This is the same except it uses the last day of the quarter.[这是相同的,只不过它使用了本季度的最后一天。]
# It requires R 2.6.0 which introduced the fromLast= argument.[要求介绍了fromLast =参数的ŕ2.6.0。]
## Not run: [#不运行:]
z.day[!duplicated(as.yearqtr(time(z.day)), fromLast = TRUE)]
## End(Not run)[#(不执行)]
# The aggregated series above are of class "zoo" (because z.day[聚合类的“动物园”系列以上的(因为z.day]
# was "zoo"). To create a regular series of class "zooreg",[“动物园”)。为了创建一个定期的类“zooreg”,]
# the frequency can be automatically chosen[的频率可以被自动选择]
zr.qtr1 <- aggregate(z.day, as.yearqtr, mean, regular = TRUE)
# or specified explicitely[或指定显式地]
zr.qtr2 <- aggregate(z.day, as.yearqtr, mean, frequency = 4)
## aggregate on month and extend to monthly time series[#总上月,延长到月度时间序列]
if(require(chron)) {
y <- zoo(matrix(11:15, nrow = 5, ncol = 2), chron(c(15, 20, 80, 100, 110)))
colnames(y) <- c("A", "B")
# aggregate by month using first of month as times for coarser series[总的月第一月的时间使用较粗系列]
# using first day of month as repesentative time[repesentative时间为每月的第一天]
y2 <- aggregate(y, as.Date(as.yearmon(time(y))), head, 1)
# fill in missing months by merging with an empty series containing[填写缺少个月的合并空系列]
# a complete set of 1st of the months[一套完整的月份第一]
yrt2 <- range(time(y2))
y0 <- zoo(,seq(from = yrt2[1], to = yrt2[2], by = "month"))
merge(y2, y0)
}
# given daily series keep only first point in each month at[每天系列只保留在每月的第一个点在]
# day 21 or more[第21天或更多]
z <- zoo(101:200, as.Date("2000-01-01") + seq(0, length = 100, by = 2))
zz <- z[as.numeric(format(time(z), "%d")) >= 21]
zz[!duplicated(as.yearmon(time(zz)))]
# same except times are of "yearmon" class[相同的,只是现在的时间是“yearmon”级]
aggregate(zz, as.yearmon, head, 1)
# aggregate POSIXct seconds data every 10 minutes[总POSIXct秒的数据,每10分钟]
tt <- seq(10, 2000, 10)
x <- zoo(tt, structure(tt, class = c("POSIXt", "POSIXct")))
aggregate(x, time(x) - as.numeric(time(x)) %% 600, mean)
# aggregate weekly series to a series with frequency of 52 per year[总的一系列频率每年52%的每周系列]
set.seed(1)
z <- zooreg(1:100 + rnorm(100), start = as.Date("2001-01-01"), deltat = 7)
# new.freq() converts dates to a grid of freq points per year[new.freq()将日期以每年一格的频率点]
# yd is sequence of dates of firsts of years[第一年的日期码序列]
# yy is years of the same sequence[yy是相同的序列年的]
# last line interpolates so dates, d, are transformed to year + frac of year[最后一行插入日期,D,转化为年+压裂一年]
# so first week of 2001 is 2001.0, second week is 2001 + 1/52, third week[2001.0的是2001年的第一周,第二周是2001 + 1/52,第三周]
# is 2001 + 2/52, etc.[2001 + 2/52,等等。]
new.freq <- function(d, freq = 52) {
y <- as.Date(cut(range(d), "years")) + c(0, 367)
yd <- seq(y[1], y[2], "year")
yy <- as.numeric(format(yd, "%Y"))
floor(freq * approx(yd, yy, xout = d)$y) / freq
}
# take last point in each period[最后一点,在每一个时期]
aggregate(z, new.freq, tail, 1)
# or, take mean of all points in each[,或采取所有点的平均值,每个]
aggregate(z, new.freq, mean)
# example of taking means in the presence of NAs[示例的装置,在定居存在]
z.na <- zooreg(c(1:364, NA), start = as.Date("2001-01-01"))
aggregate(z.na, as.yearqtr, mean, na.rm = TRUE)
# Find the sd of all days that lie in any Jan, all days that lie in[在任何一月的岁月,怀着找到SD在于,所有的日子]
# any Feb, ..., all days that lie in any Dec (i.e. output is vector with[任何二月,......,所有的岁月,怀着在任何12月(即输出向量,]
# 12 components)[12个组件)]
aggregate(z, format(time(z), "%m"), sd)
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|