2012-08-09 69 views
0

我希望(算术)平均每日数据,从而将我的每日时间序列转换为每周一次。我正在使用xts库。如何将每日时间系列转换为平均每周?

# Averages daily time series into weekly time series 
# where my source is a zoo object 
source.w <- apply.weekly(source, colMeans) 

我遇到的问题是它的平均值是星期二到下一个星期一数据的平均值。

我正在寻找选项来平均我的日常数据从星期一到星期五。

任何提示?

下面是一个比较:

# here is part of my data, from a "blé colza.txt" file 


    24/07/2012 250.5 499 
    23/07/2012 264.75 518.25 
    20/07/2012 269.25 525.25 
    19/07/2012 267 522.5 
    18/07/2012 261.25 517 
    17/07/2012 265.75 522.25 
    16/07/2012 264.25 523.25 
    13/07/2012 258.25 517 
    12/07/2012 253.75 513 
    11/07/2012 246.25 512.75 
    10/07/2012 248 515 
    09/07/2012 247 519.25 
    06/07/2012 243.25 508.25 
    05/07/2012 245 508.5 
    04/07/2012 236 500.5 
    03/07/2012 234 497.75 
    02/07/2012 234.25 489.75 
    29/06/2012 229 490.25 
    28/06/2012 229.75 487.25 
    27/06/2012 229.75 493 
    26/06/2012 226.5 486 
    25/06/2012 220 482.25 
    22/06/2012 214.25 472.5 
    21/06/2012 212 469.5 
    20/06/2012 210.25 473.75 
    19/06/2012 208 472.75 
    18/06/2012 206.75 462.5 
    15/06/2012 203 456.5 
    14/06/2012 205.25 460.5 
    13/06/2012 205.25 465.25 
    12/06/2012 205.25 469 
    11/06/2012 208 471.5 
    08/06/2012 208 468.5 
    07/06/2012 208 471.25 
    06/06/2012 208 467 
    05/06/2012 208 458.75 
    04/06/2012 208 457.5 
    01/06/2012 208 463.5 
    31/05/2012 208 466.75 
    30/05/2012 208 468 
    29/05/2012 212.75 469.75 
    28/05/2012 212.75 469.75 
    25/05/2012 212.75 465.5 



# Loads external libraries 
library("zoo") # or require("zoo") 
library("xts") # or require("xts") 

# Loads data as a zoo object 
source <- read.zoo("blé colza.txt", sep=",", dec=".", header=T, na.strings="NA",  format="%d/%m/%Y") 

# Averages daily time series into weekly time series 
# https://stackoverflow.com/questions/11129562/how-does-one-compute-the-mean-of-weekly- data-by-column-using-r 
source.w <- apply.weekly(source, colMeans) 
+1

它对我使用Mon-Sun:'data(sample_matrix); apply.weekly(sample_matrix,colMeans)'。你能提供一个[可重现的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – 2012-08-09 21:31:41

+1

是否有时区问题? – 2012-08-09 21:42:30

+0

感谢您的回复,我在原始邮件末尾添加了部分代码 – tagoma 2012-08-09 21:51:26

回答

0

再次审视我手边的问题。

它使用xts库很直接。

# say you have xts object name 'dat' 
ep <- endpoints(dat, on = 'weeks')       # 
period.apply(x = dat, INDEX = ep, FUN = mean) 
2

我跑你的例子,如果我理解正确的问题,apply.weekly功能与您的数据的第一个星期一聚集的第一个星期五。我不使用xts软件包,所以其他人不得不提供更多的信息。我会将日期转换为日期向量,每周星期一的日期代表该周的每次观察。 ?strptime摘要我用于转换的代码。

# Get the year of the first observation 
start_year <- format(time(source)[1],"%Y") 
# Convert this into a date for the 1st of Jan in that year. 
start_date <- as.Date(strptime(paste(start_year, "1 1"), "%Y %d %m")) 

# Using the difftime function determine the distance (days) since the first day of the first year. 
jul_day <- as.numeric(difftime(time(source),start_date),units="days") 
# Get the date of the Monday before each observation and add it to the start of the year. 
mondays <- start_date + (jul_day - (jul_day-1)%%7) 
# the %% calculates the remainder. 
# to check that it has worked convert the mondays vector into day names. 
format(mondays, "%A") 

# And now you can aggregate the observations using the mondays vector. 
source.w <- aggregate(source[,1:2], mondays, "mean") 
3

我能够重现你的问题,你可以用它和period.apply()定制的“终点”解决。

首先,您提供的数据,以其他人可以轻松阅读的格式。

temp = structure(list(V1 = structure(c(33L, 32L, 29L, 27L, 25L, 23L, 
22L, 19L, 17L, 15L, 13L, 12L, 9L, 7L, 5L, 3L, 2L, 41L, 39L, 37L, 
36L, 35L, 31L, 30L, 28L, 26L, 24L, 21L, 20L, 18L, 16L, 14L, 11L, 
10L, 8L, 6L, 4L, 1L, 43L, 42L, 40L, 38L, 34L), .Label = c("01/06/2012", 
"02/07/2012", "03/07/2012", "04/06/2012", "04/07/2012", "05/06/2012", 
"05/07/2012", "06/06/2012", "06/07/2012", "07/06/2012", "08/06/2012", 
"09/07/2012", "10/07/2012", "11/06/2012", "11/07/2012", "12/06/2012", 
"12/07/2012", "13/06/2012", "13/07/2012", "14/06/2012", "15/06/2012", 
"16/07/2012", "17/07/2012", "18/06/2012", "18/07/2012", "19/06/2012", 
"19/07/2012", "20/06/2012", "20/07/2012", "21/06/2012", "22/06/2012", 
"23/07/2012", "24/07/2012", "25/05/2012", "25/06/2012", "26/06/2012", 
"27/06/2012", "28/05/2012", "28/06/2012", "29/05/2012", "29/06/2012", 
"30/05/2012", "31/05/2012"), class = "factor"), V2 = c(250.5, 
264.75, 269.25, 267, 261.25, 265.75, 264.25, 258.25, 253.75, 
246.25, 248, 247, 243.25, 245, 236, 234, 234.25, 229, 229.75, 
229.75, 226.5, 220, 214.25, 212, 210.25, 208, 206.75, 203, 205.25, 
205.25, 205.25, 208, 208, 208, 208, 208, 208, 208, 208, 208, 
212.75, 212.75, 212.75), V3 = c(499, 518.25, 525.25, 522.5, 517, 
522.25, 523.25, 517, 513, 512.75, 515, 519.25, 508.25, 508.5, 
500.5, 497.75, 489.75, 490.25, 487.25, 493, 486, 482.25, 472.5, 
469.5, 473.75, 472.75, 462.5, 456.5, 460.5, 465.25, 469, 471.5, 
468.5, 471.25, 467, 458.75, 457.5, 463.5, 466.75, 468, 469.75, 
469.75, 465.5)), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, 
-43L)) 

我们会来清理并将对象转换为xts对象。

temp$V1 = as.Date(temp$V1, format="%d/%m/%Y") 
library(xts) 
temp.x = xts(temp[-1], order.by=temp$V1) 

现在。我们尝试apply.weekly()函数,但它不会给你我想要的。

apply.weekly(temp.x, colMeans) 
#    V2  V3 
# 2012-05-28 212.75 467.625 
# 2012-06-04 208.95 465.100 
# 2012-06-11 208.00 467.400 
# 2012-06-18 205.10 462.750 
# 2012-06-25 212.90 474.150 
# 2012-07-02 229.85 489.250 
# 2012-07-09 241.05 506.850 
# 2012-07-16 254.10 516.200 
# 2012-07-23 265.60 521.050 
# 2012-07-24 250.50 499.000 

要使用period.apply()你需要指定时间段的终点(可以是不规则的)。在这里,我们的第一段时间只是第一次约会,从那里开始,每隔五天。剩下几天,所以我们添加nrow(temp.x)作为我们最后一期的结束。

ep = c(0, seq(1, nrow(temp.x), by = 5), nrow(temp.x)) 
period.apply(temp.x, INDEX = ep, FUN = colMeans) 
#     V2  V3 
# 2012-05-25 212.750 465.500 
# 2012-06-01 209.900 467.550 
# 2012-06-08 208.000 464.600 
# 2012-06-15 205.350 464.550 
# 2012-06-22 210.250 470.200 
# 2012-06-29 227.000 487.750 
# 2012-07-06 238.500 500.950 
# 2012-07-13 250.650 515.400 
# 2012-07-20 265.500 522.050 
# 2012-07-24 257.625 508.625 
+1

您的评论有错误。 xts索引与所有其他R函数(ISO字符串索引除外)相同。另外,您的'ep'对象与我系统上'endpoints'的输出完全相同,所以您的解决方案与'apply.weekly'的输出相同。简而言之,您的解决方案仅适用于您与OP共享的一些内容(区域设置,时区等)。 – 2012-08-10 14:44:20

+0

@JoshuaUlrich,我已经删除了我的评论,以避免任何人后来发生误传。我对时间序列没有太多的工作,所以我老实说没有注意时区。当我运行'Sys.timezone()'时,我得到了'[1]“”'。也许这就是原因。 – A5C1D2H2I1M1N2O1R2T1 2012-08-10 16:40:32

+0

不用担心。时区可能令人沮丧。在这种情况下,它们是一个问题,因为xts将索引存储为POSIXct对象(即使它只是一个日期),所以时区是很重要的。 – 2012-08-10 17:00:17

6

mrdwabanswer只发生工作,因为它们共享一个时区(或其特性)与OP。为了说明:

Lines <- 
    "24/07/2012 250.5 499 
    23/07/2012 264.75 518.25 
    20/07/2012 269.25 525.25 
    19/07/2012 267 522.5 
    18/07/2012 261.25 517 
    17/07/2012 265.75 522.25 
    16/07/2012 264.25 523.25 
    13/07/2012 258.25 517 
    12/07/2012 253.75 513 
    11/07/2012 246.25 512.75 
    10/07/2012 248 515 
    09/07/2012 247 519.25 
    06/07/2012 243.25 508.25 
    05/07/2012 245 508.5 
    04/07/2012 236 500.5 
    03/07/2012 234 497.75 
    02/07/2012 234.25 489.75 
    29/06/2012 229 490.25 
    28/06/2012 229.75 487.25 
    27/06/2012 229.75 493 
    26/06/2012 226.5 486 
    25/06/2012 220 482.25 
    22/06/2012 214.25 472.5 
    21/06/2012 212 469.5 
    20/06/2012 210.25 473.75 
    19/06/2012 208 472.75 
    18/06/2012 206.75 462.5 
    15/06/2012 203 456.5 
    14/06/2012 205.25 460.5 
    13/06/2012 205.25 465.25 
    12/06/2012 205.25 469 
    11/06/2012 208 471.5 
    08/06/2012 208 468.5 
    07/06/2012 208 471.25 
    06/06/2012 208 467 
    05/06/2012 208 458.75 
    04/06/2012 208 457.5 
    01/06/2012 208 463.5 
    31/05/2012 208 466.75 
    30/05/2012 208 468 
    29/05/2012 212.75 469.75 
    28/05/2012 212.75 469.75 
    25/05/2012 212.75 465.5" 

# Get R's timezone information (from ?Sys.timezone) 
tzfile <- file.path(R.home("share"), "zoneinfo", "zone.tab") 
tzones <- read.delim(tzfile, row.names = NULL, header = FALSE, 
    col.names = c("country", "coords", "name", "comments"), 
    as.is = TRUE, fill = TRUE, comment.char = "#") 

# Run the analysis on each timezone 
out <- list() 
library(xts) 
for(i in seq_along(tzones$name)) { 
    tzn <- tzones$name[i] 
    Sys.setenv(TZ=tzn) 
    con <- textConnection(Lines) 
    Source <- read.zoo(con, format="%d/%m/%Y") 
    out[[tzn]] <- apply.weekly(Source, colMeans) 
} 

现在你可以运行head(out,5)和看到一些输出有所不同基于使用的时区:

head(out,5) 
$`Europe/Andorra` 
       V2  V3 
2012-05-27 212.75 467.625 
2012-06-03 208.95 465.100 
2012-06-10 208.00 467.400 
2012-06-17 205.10 462.750 
2012-06-24 212.90 474.150 
2012-07-01 229.85 489.250 
2012-07-08 241.05 506.850 
2012-07-15 254.10 516.200 
2012-07-22 265.60 521.050 
2012-07-23 250.50 499.000 

$`Asia/Dubai` 
       V2  V3 
2012-05-27 212.75 467.625 
2012-06-03 208.95 465.100 
2012-06-10 208.00 467.400 
2012-06-17 205.10 462.750 
2012-06-24 212.90 474.150 
2012-07-01 229.85 489.250 
2012-07-08 241.05 506.850 
2012-07-15 254.10 516.200 
2012-07-22 265.60 521.050 
2012-07-23 250.50 499.000 

$`Asia/Kabul` 
       V2  V3 
2012-05-27 212.75 467.625 
2012-06-03 208.95 465.100 
2012-06-10 208.00 467.400 
2012-06-17 205.10 462.750 
2012-06-24 212.90 474.150 
2012-07-01 229.85 489.250 
2012-07-08 241.05 506.850 
2012-07-15 254.10 516.200 
2012-07-22 265.60 521.050 
2012-07-23 250.50 499.000 

$`America/Antigua` 
       V2  V3 
2012-05-25 212.750 465.500 
2012-06-01 209.900 467.550 
2012-06-08 208.000 464.600 
2012-06-15 205.350 464.550 
2012-06-22 210.250 470.200 
2012-06-29 227.000 487.750 
2012-07-06 238.500 500.950 
2012-07-13 250.650 515.400 
2012-07-20 265.500 522.050 
2012-07-24 257.625 508.625 

$`America/Anguilla` 
       V2  V3 
2012-05-25 212.750 465.500 
2012-06-01 209.900 467.550 
2012-06-08 208.000 464.600 
2012-06-15 205.350 464.550 
2012-06-22 210.250 470.200 
2012-06-29 227.000 487.750 
2012-07-06 238.500 500.950 
2012-07-13 250.650 515.400 
2012-07-20 265.500 522.050 
2012-07-24 257.625 508.625 

更强大的解决方案是,以确保您的时区是正确表示,通过使用Sys.setenv(TZ="<yourTZ>")来全局设置它,或者使用indexTZ(Source) <- "<yourTZ>"来为每个单独的对象设置它。

+0

对于我的“幸运”答案,我的立场很好! (当我回答问题时,今天我学习了一些关于'端点'的东西 - 这就是为什么我喜欢这样的原因。)从我身边转移绿色复选标记是不可能的,是吗? OP,当你回来... – A5C1D2H2I1M1N2O1R2T1 2012-08-10 16:52:26

1

Joshua Ulrich的回答后续行动。

在我的系统(Kubuntu的12),下面没有检索zone.tab文件

tzfile <- file.path(R.home("share"), "zoneinfo", "zone.tab") 

不过,我能找到zone.tab由

locate zone.tab 

出于某种原因(可能是文件许可),我无法直接指向zone.tab文件,即:写入:

tzfile <- "usr/share/zoneinfo/zone.tab" 

返回斯内德:

Error in file(file, "rt") : cannot open the connection 
In addition: Warning message: 
In file(file, "rt") : 
    cannot open file 'usr/share/zoneinfo/zone.tab': No such file or directory 

问题作出zone.tab的本地副本,并指向该副本后问题:

tzfile <- "~/R/zone.tab" 

现在,如果你谷歌为zone.tab,你会发现区域的副本.tab在线,以防万一您的系统没有或者它已经损坏或者其他。这里是这样一个地方:

http://www.ietf.org/timezones/data/zone.tab 

p.S.我是< 15,所以我不能发表评论,这是我本来会做的。

相关问题